1

I am trying to access a PHP variable in JavaScript. I've escaped quotes in the PHP variable and the JavaScript won't pick it up. Without changing the $a1 php variable, how can I access it in JavaScript?

<?php

$a1 = "Here is the \"best\" apple around";    //<-- doesn't work in javascript...
$a2 = "Here is the best apple around";        //<--works fine in javascript...

?>

JavaScript:

<script type="text/javascript">

var str = "<?php echo $a1; ?>";
alert(str);

</script>
4
  • Look at this stackoverflow.com/questions/20628262/… Commented Aug 16, 2014 at 21:54
  • <?php echo addslashes($a1);?> Commented Aug 16, 2014 at 21:56
  • Well, the easiest way would be to use ' in both the PHP and the Javascript, e.g., codepad.org/SI71916p Commented Aug 16, 2014 at 21:56
  • 2
    I recommend using json_encode for simplicity and consistency. Commented Aug 16, 2014 at 21:56

3 Answers 3

4

Use json_encode for simplicity and consistency. See the ideone example.

<?php
    $a1 = "Here is the \"best\" apple around"; 
?>
var str = <?php echo json_encode($a1); ?>;

Result:

var str = "Here is the \"best\" apple around";    

Don't supply quotes in the JavaScript itself, as those come from the encoded result as required.

Advantages:

  • Correct. The result always represents a valid JavaScript literal (or object/array) expression.
  • Simple. No need to worry about quotes in JavaScript when dealing with string values.
  • Consistent. The same approach works for many values - e.g. arrays (indexed, keyed, and complex), numbers, booleans, NULL - and preserves more type information.
  • Secure. Using json_encode prevents script-injection, with the default options.
Sign up to request clarification or add additional context in comments.

3 Comments

You say the output of json_encode is invalid, is that because it is a string rather than an object or an array? They changed that in the latest version of the JSON spec so that a json text can be any json value (surrounded by any amount of whitespace).
It's just so that parsers don't fall over when you have a new line at the end of the file / indentation / etc.
@Quentin Thanks again for the update - and yes, the original aside was because I was not aware of spec changes. It's nice to see that that JSON now encompasses primitive values at the top-level.
-1

Depending on where you want to output your "str" ( in an alert or on a html page ) i would take different commands.

<script type="text/javascript">

var str = "<?php echo addslashes($a1); ?>";
alert(str);

</script>

If you would like to add it as a new DOM element i would prefer htmlspecialchars or htmlentities .

As an alternative:

var str = <?php echo json_encode($a1, JSON_UNESCAPED_UNICODE); ?>;

Comments

-2

use htmlspecialchars to convert $a1

$a1 = htmlspecialchars("Here is the \"best\" apple around"); 

1 Comment

Not appropriate for use within a SCRIPT element.

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.