1

I have made some silly error, but can not work out what I have done.

I am attempting to test passing variables from PHP to Javascript and if it is an array, json_encode it

My file is a PHP file ie .php

The php line of code that seems to be causing the error I have added to the original PHP and it works OK

<?php

$php_var = 'lol';
$php_array = array ();
$php_array["lady"] = "mary";
$php_array["gent"] = "joseph";
echo is_array($php_array) ? json_encode($php_array) : $php_array;  // same as faulty line in javascript
?>

<html>
<body>

<script type="text/javascript" charset="utf-8">

var php_var = "<?php if (is_array($php_var)) {echo json_encode($php_var); } else { echo $php_var;}; ?>";
document.write(php_var + ' ifElse<br>');

// THE FOLLOWING LINE GIVES  Uncaught SyntaxError: Unexpected identifier 
var php_var2 = "<?php echo is_array($php_array) ? json_encode($php_array) : $php_array; ?>";

document.write (php_var2 + ' EitherOR<br>');

alert(php_var + php_array);

</script>
<h1> Testing Jscript variables</h1>
</body>
</html>
mcl
  • 691
  • 2
  • 10
  • 23
  • 2
    JSON contains double quotes to begin with, which is breaking your JS. – Blender Jan 25 '13 at 11:35
  • 1
    I guess it is because of quotes in the JSON string. Remove the surrounding quotes. – VisioN Jan 25 '13 at 11:35
  • What error & Which Line? – Edwin Alex Jan 25 '13 at 11:35
  • Guys, he specified it in the source `// THE FOLLOWING LINE GIVES Uncaught SyntaxError: Unexpected identifier `. – h2ooooooo Jan 25 '13 at 11:36
  • 1
    @h2ooooooo true, I've overlooked it, though it's still ... to post an error in the source and not mentioning it in the text – dualed Jan 25 '13 at 11:39
  • possible duplicate of [Pass a PHP string to a Javascript variable (and escape newlines)](http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-and-escape-newlines) – Yoshi Jan 25 '13 at 11:39

3 Answers3

2

As you have specified the error is at :

var php_var2 = "<?php echo is_array($php_array) ? json_encode($php_array) : $php_array; ?>";

The error may be due to your using double quotes ("") use single quotes ('') in Javascript.

This may solve your error : var php_var2 = '<?php echo is_array($php_array) ? json_encode($php_array) : $php_array; ?>';

Or you can directly create an Javascript Object from the JSON string using eval().

http://jsfiddle.net/jduGp/

vedarthk
  • 1,253
  • 1
  • 14
  • 34
  • Thanks for all the comments/answers - the single quote solved it. I have learnt a bit more about interfacing PHP and Javascript. – mcl Jan 25 '13 at 12:11
0

Try this code. Replace the two lines as shown below.

var php_var = <?php if (is_array($php_var)) {echo json_encode($php_var); } else { echo $php_var;}; ?>;

var php_var2 = <?php echo is_array($php_array) ? json_encode($php_array) : $php_array; ?>;
Techie
  • 44,706
  • 42
  • 157
  • 243
0

I did it like this

<?php

$php_var = 'lol';
$php_array = array ();
$php_array["lady"] = "mary";
$php_array["gent"] = "joseph";
?>

<html>
<body>

<script type="text/javascript" charset="utf-8">

var php_var = <?php if (is_array($php_var)) {echo json_encode($php_var); } else { echo '"' . $php_var . '"';} ?>;
document.write(php_var + ' ifElse<br>');

// THE FOLLOWING LINE GIVES  Uncaught SyntaxError: Unexpected identifier
var php_var2 = <?php echo is_array($php_array) ? json_encode($php_array) : $php_array; ?>;

document.write (php_var2 + ' EitherOR<br>');

alert(php_var);

</script>
<h1> Testing Jscript variables</h1>
</body>
</html>

Not sure why you're trying to alert out php_array when Javascript isn't aware of that variable. You also don't need the quotes unless you're outputting a string. If you put quotes around an object Javascript will think it's a string.

And Finally
  • 5,602
  • 14
  • 70
  • 110
  • When I tried it without quotes it gave the following `Uncaught ReferenceError: lol is not defined` – mcl Jan 25 '13 at 12:14
  • When I remove the quotes from around the outer braces: var php_var2 = ; JS seems to be happy - it just complains that php_array is undefined, which is fair enough because as far as JS is concerned it isn't. – And Finally Jan 26 '13 at 17:24