我想将php变量传递给JavaScript。我必须从数据库中获取问题(随机,逐个)以及最终用户是否正确响应, 该计划正在做其他事情。
数据库
+------+-----------------------+---------------+-----------------+
| q_id | description | text | answer |
+------+-----------------------+---------------+-----------------+
| 1 |What is the capital | United States | Washington D.C. |
| 2 |What is the capital | California | Sacramento |
| 3 |What is the capital | Maryland | Annapolis |
+------+-----------------------+---------------+-----------------+
在php文件中,变量$question
和$correctAnswer
具有以下值:
php代码:
$sql="SELECT description, text, answer FROM Questions";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$question=$row['description']."of ".$row['text']."?";
echo($question);
$correctAnswer=$row['answer'];
//echo($correctAnswer);
}
javascript代码:
var rightAnswer;
function takeQuestion()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("question").innerHTML=xmlhttp.responseText;
rightAnswer ='<?php echo $correctAnswer;?>';
alert(rightAnswer);
}
}
xmlhttp.open("GET","getQuestion.php",true);
xmlhttp.send();
}
程序随机打印问题,但是我在传递变量时遇到了问题
$correctAnswer
到Javascript。 JavasScript中的变量rightAnswer
应该取值
php变量$correctAnswer
。任何帮助将不胜感激。提前谢谢!
答案 0 :(得分:2)
在PHP里面while循环中执行此操作
$question=$row['description']."of ".$row['text']."?|".$row['answer'];
echo($question);
你的java脚本中的
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var elements = xmlhttp.responseText.split("|");
document.getElementById("question").innerHTML=elements[0];
var rightAnswer = elements[1];
alert(rightAnswer);
}
这是一个想法。将其更改为您想要的方式。
答案 1 :(得分:1)
将您的javascript更改为:
将其称为responseXml:
<script type="text/javascript">
var rightAnswer;
function takeQuestion()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
xmlDoc=xmlhttp.responseXML;
document.getElementById("question").innerHTML=xmlDoc.getElementsByTagName('question')[0].firstChild.nodeValue;
rightAnswer =xmlDoc.getElementsByTagName('answer')[0].firstChild.nodeValue;
alert(rightAnswer);
}
}
xmlhttp.open("GET","getQuestion.php",true);
xmlhttp.send();
}
</script>
将您的php文件更改为:
<?php
header('Content-Type: text/xml');
header ('Cache-Control: no-cache');
header ('Cache-Control: no-store' , false); // false => this header not override the previous similar header
$sql="SELECT description, text, answer FROM Questions";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$question=$row['description']."of ".$row['text']."?";
//echo($question);
$correctAnswer=$row['answer'];
//echo($correctAnswer);
}
$xmlStr='<?xml version="1.0" encoding="UTF-8"?>
<data>
<question>'.$question.'</question>
<answer>'.$correctAnswer.'</answer>
</data>
';
echo $xmlStr;
?>
答案 2 :(得分:0)
当你在同一页面上有Javascript和PHP时,你可以使用json_encode将数据传递给javascript:
<?php
$var = 'hello';
?>
<script type="text/javascript">
var v = <?= json_encode($var); ?>;
</script>
没有引号
如果你进行ajax调用 - 变量不是通过PHP传递的,而是作为http请求传递的。在这种情况下你使用普通的JS