I want to pass a php variable into JavaScript. I have to get from a database the questions(randomly, one by one) and if the end user responses correctly, the program is doing something else.
database
+------+-----------------------+---------------+-----------------+
| 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 |
+------+-----------------------+---------------+-----------------+
In the php file, variables $question and $correctAnswer have the following values:
php code:
$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 code:
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();
}
The program is printing randomly the questions, but I have problems passing the variable
$correctAnswer to Javascript. Variable rightAnswer in JavasScript should take the value
of php variable $correctAnswer. Any help will be appreciated. Thank you in advance!
-
you need to send answer along with question from getQuestion.phpGBD– GBD2012年11月11日 16:47:32 +00:00Commented Nov 11, 2012 at 16:47
-
what js error you are gettingWaqar Alamgir– Waqar Alamgir2012年11月11日 16:53:59 +00:00Commented Nov 11, 2012 at 16:53
-
in the alert box I have: <?php echo $correctAnswer;?>; if I do not use quotes, the error is: Uncaught SyntaxError: Unexpected token ?Lavinia– Lavinia2012年11月11日 16:56:48 +00:00Commented Nov 11, 2012 at 16:56
3 Answers 3
In your PHP inside while loop do this
$question=$row['description']."of ".$row['text']."?|".$row['answer'];
echo($question);
in your java script
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var elements = xmlhttp.responseText.split("|");
document.getElementById("question").innerHTML=elements[0];
var rightAnswer = elements[1];
alert(rightAnswer);
}
This is an idea. Change it to way that you want.
Comments
Change Your javascript as:
Call it as 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>
Change Your php file as:
<?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;
?>
6 Comments
when you have Javascript and PHP on the same page, then you can pass data to the javascript using json_encode:
<?php
$var = 'hello';
?>
<script type="text/javascript">
var v = <?= json_encode($var); ?>;
</script>
without quotes
if you make an ajax call - the variable is not passed through the PHP, but as an http request. You use plain JS in that case