1

I got a syntax error on the following code. Once I took out the first line of code, the alert pops. My brain is almost dead and I can't find out why. Please help. Thanks a lot.

JS

var rss = <?php echo json_encode($test); ?>;

alert("Hello World");

Updated

html

  <?php 

    $test=get_array(); 

   //the $test is a multi-dimention array.

  ?>


<script type="text/javascript" src="js/slideshow.js"></script>
3
  • Could you please post the json_encode function output? Commented Jul 5, 2012 at 5:16
  • can you please post what you are getting in view source or what you have in $test? Commented Jul 5, 2012 at 5:16
  • It's a multi-dimen array. Not sure if that's the reason. please see updated question. Thanks a lot. Commented Jul 5, 2012 at 5:24

4 Answers 4

5

json_encode will return FALSE on failure, so if it fails, echo false will output nothing.

so your code became below, which give you a syntax error.

var rss = ;

alert("Hello World");

Edit: From your updated edit, the reason is: You can not write php code in a js file.

If you need that variable in the js file, assign it to a global variable first.

<script type="text/javascript">
  var rss = <?php echo json_encode($test); ?>;
</script>
<script type="text/javascript" src="js/slideshow.js"></script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. I am trying to bring my php array to my JS file. Please see my updated question. +1 though.
For reference, it's possible to tell the server that JS files should be parsed by PHP (which would let you put PHP code in them). But it's hardly ever done, because that means every script gets run through the interpreter (which is a lot of wasted CPU time, if most JS files are just JS).
2

To avoid error, your code should be:

var rss = <?php echo json_encode($test) ? json_encode($test) : "''"; ?>;
alert("Hello World");

Even if json_encode returns false, your code would be:

var rss = '';
alert("Hello World");

2 Comments

that's a good solution to avoid the error. I am not sure why my $test is invalid. Please see my updated php. +1 though.
Note, if the PHP interpreter is braindead (and i fully expect it to be), it'll end up running json_encode twice. Oh, and you may want to quote your '' so that PHP doesn't still spit out nothing. :)
1

This is because JavaScript throws fatal error and stops current script execution because of:

var rss = <?php echo json_encode($test); ?>; 

The problem is that this may return non-Valid JSON string and it throws new exception,

What you can do:

First of all you should determine if the string you got from PHP is actually valid. As JSON methods usually do throw errors, you can simply put this var into try/catch block

try {

  var rss = <?php echo json_encode($your_array); ?>;

} catch(e){

  alert('Invalid JSON string');
}

In PHP, please also check if it's valid to prevent sending invalid JSON string back to javaScipt

5 Comments

If you are talking about PHP - then it's true. but in JS it does catch
@cHao From your example - This is fatal error that does not handle any object. not JSON.parse, for example. The problem is as aforementioned - javascript throws FATAL error that's why the next line (alert()) doesn't work.
No one's talking about JSON.parse. The problem is that json_encode isn't spitting out anything (or maybe isn't even being run by PHP), making the actual JS code invalid. (The browser would actually see var rss = ; or var rss = <?php echo json_encode($test); ?>;.) And if the JS code itself is invalid, a catch block isn't going to catch it, cause it doesn't even know it should, because the code hasn't been parsed yet.
@cHao oh the problem was : He wrote PHP code into js file, that's why haven't been parsed by PHP. Anyway, if JS code itself isn't valid - I didn'n mean it to be catched, instead JavaScript will stop current execution anyway
That's the entire problem, though. The way the code is written, any "invalid JSON" (including, say, some PHP code tossed into the script) will cause a syntax error, cause it's not JSON in this case -- it's an object literal. So the catch block will never catch anything.
1

In a .js file, PHP doesn't work. It will work only in files with extension .php . Put your JS code in the PHP page; then it works.

Comments

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.