-1

I am Trying to escape these darn quotes and stuff in JS. I'm trying to use json_encode. My head hurts from looking at quotes, help???

  $list = '';

    // some loop here

   $message = 'centeredPopup(this.href,"myWindow","500","300","yes")';
   $jscode = 'json_encode('.$message.');return false';
   $list .= '<p> 
        <a href="http://www.example.com/something.php?id=' . $id . '" onclick="' 
              .htmlspecialchars($jscode) . '" >' . $name . '</a></p><br>';

4
  • Why are you putting the javascript in PHP variables to begin with? Commented Apr 9, 2014 at 4:52
  • I have no idea what you're trying to do. json_encode() is not javascript, so why encapsulate it in a string? Commented Apr 9, 2014 at 4:52
  • i was using this example. stackoverflow.com/questions/7085648/… i'm generating a list of links that create popup windows or was trying to. this may not be the best way though ^_^ Commented Apr 9, 2014 at 4:57
  • 1
    I would say rather than putting a long string of javascript inside a onClick attribute, make a named javascript function and have your onClick reference it. Commented Apr 9, 2014 at 5:09

2 Answers 2

1

If you want to call json_encode there, you need to do this.

$jscode = json_encode($message).';return false';

You don't even need json_encode for this. You can put this there:

$message = 'centeredPopup(this.href,"myWindow","500","300","yes")';
$jscode = $message.';return false';
$list .= '<p> <a href="http://www.example.com/something.php?id=' . $id . '" onclick="' .htmlspecialchars($jscode) . '" >' . $name . '</a></p><br>';
Sign up to request clarification or add additional context in comments.

Comments

0

Try:

$message = 'centeredPopup(this.href,"myWindow","500","300","yes")';
$jscode = json_encode($message);

Otherwise everything else should be OK.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.