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>
6
  • 2
    JSON contains double quotes to begin with, which is breaking your JS. Commented Jan 25, 2013 at 11:35
  • 1
    I guess it is because of quotes in the JSON string. Remove the surrounding quotes. Commented Jan 25, 2013 at 11:35
  • What error & Which Line? Commented Jan 25, 2013 at 11:35
  • Guys, he specified it in the source // THE FOLLOWING LINE GIVES Uncaught SyntaxError: Unexpected identifier . Commented Jan 25, 2013 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 Commented Jan 25, 2013 at 11:39

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all the comments/answers - the single quote solved it. I have learnt a bit more about interfacing PHP and Javascript.
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; ?>;

Comments

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.

2 Comments

When I tried it without quotes it gave the following Uncaught ReferenceError: lol is not defined
When I remove the quotes from around the outer braces: var php_var2 = <?php echo is_array($php_array) ? json_encode($php_array) : $php_array; ?>; 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.

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.