0

我正在尝试编写一个游戏,你掷骰子并得到一半的报价,你必须猜测它是否与你最初给出的一半相匹配。我正在使用 AJAX,一切正常,直到我将变量从 php 发送到 javascript 使用

echo  '<script>currentQuote = $currentQuote;</script>'; 

它显示在页面上,但是当我在 JS 中检查 currentQuote 的值时,它是未定义的。这是一个简化版游戏的链接,只是显示了我的问题。http://workwithu.com/Games/Game15snip.php你看不到的PHP代码是:

<?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
?>

任何帮助将不胜感激。我已经没有尝试的想法了。谢谢

4

2 回答 2

4

基本的 PHP 语法。'- 引用的字符串不会插入变量值。如果你view source在你的页面上做 a ,你会看到一个文字

<script>current Quote = $currentQuote</script>

在页面的源代码中。切换到带"引号的字符串:

echo "<script>currentQuote = $currentQuote;</script>";//this not working
     ^---note                                       ^---note
于 2013-10-24T19:45:14.517 回答
0

如果你使用 ' -Quoted sting 你应该使用。

echo '<script>currentQuote = '.$currentQuote.';</script>';

或使用 " -quoted

echo "<script>currentQuote = $currentQuote;</script>";
于 2013-10-24T19:49:55.720 回答