BWall
BWall

Reputation: 43

JSON_encode syntax error in JavaScript

I have the following code:

window.location.href = "http://www.example.com/" + <?php json_encode($row["item"]); ?>;

That's causing a ; syntax error. I understand why - it's because $row["item"] doesn't exist until it's echo'ed from another page (using echo ".$row["item"].";), so html sees it as window.location.href = "http://www.example.com/" + ;

I've been attempting to fix this for a while, and thought json_encode may do the job, but that hasn't worked either.

Upvotes: 2

Views: 102

Answers (1)

Josef Andersson
Josef Andersson

Reputation: 81

Try this:

window.location.href = "http://www.example.com/<?php echo $row["item"]; ?>";

Now, if $row["item"] is undefined, you shouldn't be getting the " + ; JavaScript error any longer.

Upvotes: 2

Related Questions