dadamssg87 Posted October 5, 2011 Share Posted October 5, 2011 I wrote a function that produces two form input fields into a div when a link is clicked. The input's default value's are passed through the javascript function. These values are produced by php. I can't figure out how to pass single quotes through it though. I'm using the addslashes() php function on the values before being placed into the onClick function. This handles double quotes fine. But a single apostrophe breaks the function. Has anybody got a clue on what i'm doing wrong? Both addslashes() and addcslashes($value, '"') work for double quotes but but not single. PHP: <?php $code = $add_ons[$id]['code']; $display_title = $add_ons[$id]['title']; $atitle = addslashes($add_ons[$id]['title']); // $atitle = addcslashes($add_ons[$id]['title'], '"'); $price = $add_ons[$id]['price']; echo "<a href='javascript:void;' onclick='add_item(\"$code\",\"$atitle\",\"$price\",\"#add_on_box\")' >$display_title</a>"; ?> Javascript: function add_item(code, title, price, divbox) { var idtag, item_title, item_price, item_code, separator; var room_code, code_text; code_text = "<div id='code_text'>" + code + "</div>"; separator = " "; // Generate an id based on timestamp idtag = "div_" + new Date().getTime(); // Generate a new div. // $(divbox).append("<div id=\"" + idtag + "\"></div>"); $('#order_box').append("<div id=\"" + idtag + "\"></div>"); if (divbox == "#property_box") { item_code = $("<input/>", { type: 'hidden', name: "property_item_code[]", value: code, class: "twelve code_input" }); item_title = $("<input/>", { type: 'text', name: "property_item_title[]", value: title, class: "twelve" }); item_price = $("<input/>", { type: 'text', name: "property_item_price[]", value: price, class: "twelve price_input right" }); // Show in the document. $("#" + idtag).append(item_code, code_text, separator, item_title, separator, item_price, separator); $("#" + idtag).append(" <a href='javascript:void;' onclick=\"return remove_it('" + idtag + "');\" class='twelve'>Remove</a>"); } else if (divbox == "#add_on_box") { item_code = $("<input/>", { type: 'hidden', name: "add_on_item_code[]", value: code, class: "twelve code_input" }); item_title = $("<input/>", { type: 'text', name: "add_on_item_title[]", value: title, class: "twelve" }); item_price = $("<input/>", { type: 'text', name: "add_on_item_price[]", value: price, class: "twelve price_input right" }); // Show in the document. $("#" + idtag).append(item_code, code_text, separator, item_title, separator, item_price, separator); $("#" + idtag).append(" <a href='javascript:void;' onclick=\"return remove_it('" + idtag + "');\" class='twelve'>Remove</a>"); } else { room_code = $("<input/>", { type: 'hidden', name: "room_item_code[]", value: code, class: "twelve code_input" }); item_title = $("<input/>", { type: 'text', name: "room_item_title[]", value: title, class: "twelve" }); item_price = $("<input/>", { type: 'text', name: "room_item_price[]", value: price, class: "twelve price_input right" }); // Show in the document. $("#" + idtag).append(room_code, code_text, separator, item_title, separator, item_price, separator); $("#" + idtag).append(" <a href='javascript:void;' onclick=\"return remove_it('" + idtag + "');\" class='twelve'>Remove</a>"); } } Then i view the source of the webpage to see whats actually being produced and i see this.. The name of this item is "I'm Frustrated". <a href='javascript:void;' onclick='add_item("AAwayY","I\'m Frustrated","25.00","#add_on_box")' >I'm Frustrated</a> Quote Link to comment https://forums.phpfreaks.com/topic/248452-passing-values-with-single-quotesapostrophes-through-a-javascript-function/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.