-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>';

Carl
  • 43,122
  • 10
  • 80
  • 104
Clam
  • 935
  • 1
  • 12
  • 24
  • Why are you putting the javascript in PHP variables to begin with? – david brainerd Apr 09 '14 at 04:52
  • I have no idea what you're trying to do. `json_encode()` is not javascript, so why encapsulate it in a string? –  Apr 09 '14 at 04:52
  • i was using this example. http://stackoverflow.com/questions/7085648/how-to-escape-string-from-php-for-javascript i'm generating a list of links that create popup windows or was trying to. this may not be the best way though ^_^ – Clam Apr 09 '14 at 04: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. – david brainerd Apr 09 '14 at 05:09

2 Answers2

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>';
PurkkaKoodari
  • 6,703
  • 6
  • 37
  • 58
0

Try:

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

Otherwise everything else should be OK.

Justin Mitchell
  • 339
  • 1
  • 5