Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6645755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:19:43+00:00 2026-05-26T00:19:43+00:00

I had stackoverflow help me with a javascript function in my last question. The

  • 0

I had stackoverflow help me with a javascript function in my last question. The function created a div, then appended two form input fields into the newly created div. The function took in 4 arguments. One of which is the value of the form input field. The problem is if a single quote(apostrophe) is in the value it breaks the function. I’m developing in PHP and using jQuery. I’ve tried addslashes(), addcslashes($text,'”‘), and json_encode() which all work great for double quotes but not single quotes. What am i doing wrong?

Link-1 is broken because of an apostrophe. Link-2 works as is, with its “Soda” value.

Example:

<?php
$text = "I'm Frustrated";
$text = addslashes($text);
?>
<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
        #item_box1{
            width: 300px;
            margin: 0 auto;
            height: 100px;
            border: 1px solid blue
        }

        #item_box2{
            width: 300px;
            margin: 0 auto;
            height: 100px;
            border: 1px solid red
        }
        .link-button {
            color: rgb(0, 0, 255);
            cursor: pointer
        }
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.3.js"></script>
    <script type="text/javascript">
        function add_item( code, title, price, divbox )
        {
            var idtag, item_title, item_price, item_code;
            // Generate an id based on timestamp
            idtag = "div_" + new Date().getTime();
            // Generate a new div.
            $( divbox ).append( "<div id=\"" + idtag + "\"></div>" );

            if ( divbox == "#item_box1" )
            {
                item_title = $("<input/>", {
                    type: 'text',
                    name: "box1_item_title[]",
                    value: title
                });

                item_price = $("<input/>", {
                    type: 'text',
                    name: "box1_item_price[]",
                    value: price
                });
                // Show in the document.
                $( "#" + idtag ).append( item_title, item_price );
            }
            else
            {
                item_code = $("<input/>", {
                    type: 'text',
                    name: "box2_item_code[]",
                    value: code
                });

                item_title = $("<input/>", {
                    type: 'text',
                    name: "box2_item_title[]",
                    value: title
                });
                // Show in the document.
                $( "#" + idtag ).append( item_code, item_title );
            }
        }
    </script>
</head>
<body>
    <a class="link-button" onclick="return add_item('xyz', "<?php echo $text;?>", '45.99', '#item_box1');">Add Item to Box 1</a>
    <br>
    <a class="link-button" onclick="return add_item('123', 'Soda', '1.99', '#item_box2');">Add Item to Box 2</a>
    <br>
    <br>
    <div id='item_box1'></div>
    <div id='item_box2'></div>

</body>

The source of the page ends up looking like this:

<a class="link-button" onclick="return add_item('xyz', "I\'m Frustrated", '45.99', '#item_box1');">Add Item to Box 1</a>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:19:43+00:00Added an answer on May 26, 2026 at 12:19 am

    In the onclick attribute of link 1, you’re using an unescaped double-quoted JavaScript string inside a double-quoted HTML attribute. The onclick attribute ends up getting set to return add_item('xyz', and the remainder of the open tag isn’t valid HTML. Changing the double quotes around the PHP tag to single quotes should fix that problem.

    The resulting attribute definition would then be
    onclick="return add_item('xyz', 'I\'m Frustrated', '45.99', '#item_box1');"
    I’m pretty sure that’s valid both for HTML and JavaScript.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following up from my last question @ stackoverflow.com/questions/7049245/ I got a couple of answers
StackOverflow user jolson had a very nice piece of code that exemplifies how one
I've had a look at Stack Overflow question Initialization of a microSD card using
In a previous question on Stack Overflow , I had run into an issue
I need some help in PHP to create short urls just like StackOverflow creates
I posted this on the JasperServer forums, but I've had better luck on StackOverflow
Im hoping you geniuses here at stackoverflow can help me with this. Recently i
I had a look at other stackoverflow questions and couldn't find one that asked
I posted this question: https://stackoverflow.com/questions/418597/java-and-net-for-php-programmer and the answers I was given didn't really help
With the help of some very gracious stackoverflow contributors in this post , I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.