-2

I'm getting the string from controller:

var x = '<?php echo addcslashes($this->x, "'") ?>';

The parsed result is:

var x = '<script>alert(\'x\')</script>';

Error:

Uncaught SyntaxError: Invalid or unexpected token

I tried to assign the string directly from JS and it works.

Bacchus
  • 515
  • 8
  • 22
  • https://stackoverflow.com/a/5895936/5541139 – Santosh Jun 12 '17 at 08:27
  • https://stackoverflow.com/a/23740549/4511459 – Nawin Jun 12 '17 at 08:28
  • Do you want your x variable to actually contain the _text_ ` – CBroe Jun 12 '17 at 08:29
  • Possible duplicate of [How to assign Php variable value to Javascript variable?](https://stackoverflow.com/questions/5895842/how-to-assign-php-variable-value-to-javascript-variable) – mickmackusa Jun 12 '17 at 09:33
  • Possible duplicate of [How to pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – Hugues M. Jun 12 '17 at 11:02

1 Answers1

0

The alert is coming up because the first character in the alert is a slash (the unexpected token) the second slash escapes the ' so the alert never closes either.

Not really sure what you're trying to achieve honestly with the script tags etc seems as you're already inside a script when it gets called (unless you're printing it to a page, in which case you're better off adding it to an event handler or something.)

Caitlin
  • 36
  • 3