Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question
3  
What error? –  dualed Jan 25 at 11:34
2  
JSON contains double quotes to begin with, which is breaking your JS. –  Blender Jan 25 at 11:35
1  
I guess it is because of quotes in the JSON string. Remove the surrounding quotes. –  VisioN Jan 25 at 11:35
 
What error & Which Line? –  Edwin Alex Jan 25 at 11:35
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 at 11:39
show 2 more comments

3 Answers

up vote 1 down vote accepted

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/

share|improve this answer
 
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 at 12:11
add comment

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; ?>;
share|improve this answer
add comment

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.

share|improve this answer
 
When I tried it without quotes it gave the following Uncaught ReferenceError: lol is not defined –  mcl Jan 25 at 12:14
 
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. –  And Finally Jan 26 at 17:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.