Jump to content

passing values with single quotes(apostrophes) through a javascript function


Recommended Posts

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.