-5

I have this string in php

$tagged

and I want to put it in my javacript script inside the ' '

tagName: '',

how do I do this

Sergio
  • 28,539
  • 11
  • 85
  • 132
user1616846
  • 181
  • 1
  • 1
  • 10

2 Answers2

1

The best way IMHO would be this:

tagName: <?php echo json_encode($tagged); ?>

This way, you don't have to take care of escaping quotes, escape characters or other unwanted signs.

ComFreek
  • 29,044
  • 18
  • 104
  • 156
1

If your view is rendered by PHP:

tagName: '<?= json_encode($tagged)?>',

If your JS code is a part of a PHP view, build the object from PHP:

$jsObject = json_encode(array('tagName' => $tagged'));
Eirik Hoem
  • 1,310
  • 7
  • 14