SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quotes in js - again.

    I have this (in php):
    $text="test'it";

    and later this:
    "<input....onclick='test(\"".$text."\");'>"

    and test() is:
    function test(a){alert(a);}

    The function doesn't display anything with "test'it" but displays "test it" ok. Htmlentities doesn't fix it and I can't find any other solution.
    Thanks

  2. #2
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    656
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    First question: Did you put <input... inside the <php?...brace? That is the only way you can add $text to the js mothod. Unless I am wrong. Someone chime in..
    Chuck Norris is so tough, mosquitos ask for permission before they bite him

  3. #3
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by USPaperchaser View Post
    First question: Did you put <input... inside the <php?...brace? That is the only way you can add $text to the js mothod. Unless I am wrong. Someone chime in..
    Yes. More fully it's:
    <?php
    $text="test'it";
    ....
    echo "<input...onclick='(\"".$text."\");'>";
    ?>
    I tried addslashes($text) as well as htmlentities but still no good.

  4. #4
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Try adding:
    $text = $text.replace(/\'/g, "&#39");

    Right after $text = "test' it";

    &#39 is the HTML code for the apostrophe. Remember, you're concatenating two strings through PHP which then get "unstringed". Lol it's confusing but here's a visualization of what's happening:
    Code:
    <input...onclick=' (\ " test' it ' " );'>
    _________________------------____-------__
    Underscores are regular characters and hyphens are strings.

  5. #5
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks OMGCarlos. I've tried it once and it didn't work, but I'll get back to it later.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •