I am trying to write a game where you roll the dice and get half a quote and you have to guess if it matches the half that you were originally given. I am using AJAX and everything works fine right up until the end when I send a variable from php to javascript using
echo '<script>currentQuote = $currentQuote;</script>';
It shows up on the page but when I check the value of currentQuote in JS it is undefined. Here is a link to a simplified version of the game just showing my problem. http://workwithu.com/Games/Game15snip.php The PHP code which you cannot see is:
<?php
$response = mt_rand(1,6);
echo $response;
$currentQuote=$response;
echo '<script>currentQuote = $currentQuote;</script>';//this not working
echo "<br>";
echo "currentQuote is:";
echo $currentQuote; //this shows on the screen fine
?>
Any help would be GREATLY appreciated. I have run out of ideas to try. Thanks
Basic PHP syntax. '
-quoted strings do NOT interpolate variable values. If you do a view source
on your page, you'll see a literal
<script>current Quote = $currentQuote</script>
in the page's source. Switch to a "
-quoted string:
echo "<script>currentQuote = $currentQuote;</script>";//this not working
^---note ^---note
if you are using ' -Quoted sting you should use.
echo '<script>currentQuote = '.$currentQuote.';</script>';
or use " -quoted
echo "<script>currentQuote = $currentQuote;</script>";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.