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, plete with garlands and incense.
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, plete with garlands and incense.
Share Improve this question edited Jan 18, 2016 at 4:23 ChangeMachine asked Jan 18, 2016 at 2:16 ChangeMachineChangeMachine 391 silver badge6 bronze badges 5-
The results of
json_encode(5)
is just going to be 5. There's really no point in usingjson_encode()
here. – DiddleDot Commented Jan 18, 2016 at 2: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 Commented Jan 18, 2016 at 2:34 - @DiddleDot, json_encode(5) will return a string of "5". My actual JSON is more plex. 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. – ChangeMachine Commented Jan 18, 2016 at 3:40
- @EdCottrell, it shows up in a ment tag. Thanks to both of you for weighing in on it. – ChangeMachine Commented Jan 18, 2016 at 3:43
-
Re: using <?php in an .html doc, I added
AddType application/x-httpd-php .htm .html
to my .htaccess to enable that, but good point. I will check that this is actually working. PHP support of .html docs – ChangeMachine Commented Jan 18, 2016 at 21:36
4 Answers
Reset to default 3var data = "<?php echo $data; ?>";
You need to add double quote to indicate $data is a json string.
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.
You need to research the difference between a server side language (PHP) and a client side language (Javascript).
They do not municate 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./questions/171203/what-are-the-differences-between-server-side-and-client-side-programming
Javascript Cookies http://www.w3schools./js/js_cookies.asp
PHP Cookies http://www.w3schools./php/php_cookies.asp
Hope this helps :)
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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744795624a4594188.html
评论列表(0条)