-2

Despite a mountain of posts on the subject, I'm having no luck getting embedded php to echo out into a JS variable. Even stripped down to:

//PHP from app.php
$data = json_encode(5);

//and JS in results.html
var data = <?php echo $data; ?>;

console.log only produces "SyntaxError: expected expression, got '<'". With quotes around the

It seems that the PHP isn't parsing. As Ed Cottrell points out below, there's a here remains unpopular and unanswered. I ran the test.php created by pemcconnell and that works for me. He says that suggests "another problem with your JS" but my JS is working just fine otherwise.

So... I throw myself to the wolves. What stupid thing am I doing? If anyone can help I will make an altar in their honor, complete with garlands and incense.

James Nielson
  • 39
  • 1
  • 6
  • The results of `json_encode(5)` is just going to be 5. There's really no point in using `json_encode()` here. – DiddleDot Jan 18 '16 at 02:20
  • Click on "view source" in your browser. Odds are you will see the PHP code in the HTML, which means that the server is not processing the file as PHP. This is extremely likely. You say the code is in `results.html`. Unless your server is configured to process .html files as PHP - unlikely and a bad idea - it's not treating that file as PHP. – elixenide Jan 18 '16 at 02:34
  • @DiddleDot, json_encode(5) will return a string of "5". My actual JSON is more complex. That's just my attempt to eliminate factors. I forgot to mention that my actual JSON passes JSLint, but produces the same error in JS. – James Nielson Jan 18 '16 at 03:40
  • @EdCottrell, it shows up in a comment tag. Thanks to both of you for weighing in on it. – James Nielson Jan 18 '16 at 03:43

4 Answers4

3
var data = "<?php echo $data; ?>";

You need to add double quote to indicate $data is a json string.

Than Ngo Hoai
  • 479
  • 3
  • 10
  • Thanks Than, but when I use quotes it just loads "" into the var without parsing it like it should. With JSON.parse on there it then gives an error of "unexpected character at line 1 column 1" or something along those lines. I should add that to the post. – James Nielson Jan 18 '16 at 03:47
  • 2
    var data = "" should be in a PHP file. So you can use PHP tag. – Than Ngo Hoai Jan 18 '16 at 04:09
2

As suggested by Than Ngo Hoai in the answer above the code has to be in a php file, not a html. Otherwise it won't work. You should accept his answer instead of yours.

Community
  • 1
  • 1
HelloWorld
  • 697
  • 10
  • 16
1

You need to research the difference between a server side language (PHP) and a client side language (Javascript).

They do not communicate hand in hand however there is a solution. Save the Javascript variable as a browser cookie and access it in PHP. Then you can echo out the cookie value or assign it to a PHP variable.

PHP vs Javascript Info https://softwareengineering.stackexchange.com/questions/171203/what-are-the-differences-between-server-side-and-client-side-programming

Javascript Cookies http://www.w3schools.com/js/js_cookies.asp

PHP Cookies http://www.w3schools.com/php/php_cookies.asp

Hope this helps :)

Community
  • 1
  • 1
Bigwater
  • 223
  • 2
  • 12
  • I'm pretty clear on server-side vs client-side, but I do have a lot to learn. Forever... :| But I am following [an accepted SO answer](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript). I may explore the cookie route, but I was hoping I could figure out what I was doing wrong with the echo method. Thanks for the resources/feedback. Much appreciated. – James Nielson Jan 18 '16 at 04:01
  • Please vote answer up or mark as correct using the tick next to answer if it helped you :) Cheers – Bigwater Jan 18 '16 at 06:14
  • Nobody would upvote you after you told user to research when he has already done so. It's quite upsetting. It disencourages even more to upvote you when you ask for votes. BUT you as everyone here put a bit of effort, so i will upvote you and asker. – Mbotet Oct 18 '19 at 12:36
0

I'm still unable to do any variation on php echo but I switched to Twig's {{ }} and it works. I had to put quotes around my entire "JSON.parse()" statement (I got desperate) to figure out that Twig WAS parsing, but was messing up the JSON by auto-escaping double-quotes. Once I fixed that with {{ myData.json_encode()|raw }} it was perfect.

As for the original issue, Idunno. My htaccess is set to recognize html docs as php. I also tried changing results.html to .php, and a million other things I've read here and there. It "should" work but it don't.

Thanks, everyone, for the feedback. It was all good advice and helped me figure things out.

James Nielson
  • 39
  • 1
  • 6