Javascript 我不知道';我不知道如何在多个div中显示响应文本
我正在制作一个测验网站,点击它从数据库中获取问题和选项 我想在一个分区中显示问题,在其他三个分区中显示选项。 我有两个页面,一个是careerguidance.php,另一个是getquestion.php Careerguidance.phpJavascript 我不知道';我不知道如何在多个div中显示响应文本,javascript,jquery,html,Javascript,Jquery,Html,我正在制作一个测验网站,点击它从数据库中获取问题和选项 我想在一个分区中显示问题,在其他三个分区中显示选项。 我有两个页面,一个是careerguidance.php,另一个是getquestion.php Careerguidance.php <head> <script> var str=0; function showquestion() { if (window.XMLHttpRequest) {
<head>
<script>
var str=0;
function showquestion() {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("div1").innerHTML = xmlhttp.responseText;
// document.getElementById("dop1").innerHTML = xmlhttp.responseText;
// document.getElementById("dop2").innerHTML = xmlhttp.responseText;
// document.getElementById("dop3").innerHTML = xmlhttp.responseText;
}
}
str++;
xmlhttp.open("GET","getquestion.php?idd="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<button onClick="showquestion()">Next</button>
<div class='quiz' id='div1'><h2>$question</h2></div>
<div class='quizoption' id='dop1' onClick=''><h4></h4></div>
<div class='quizoption' id='dop2' onClick=''><h4></h4></div>
<div class='quizoption' id='dop3' onClick=''><h4></h4></div>
</body>
var-str=0;
函数showquestion(){
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“div1”).innerHTML=xmlhttp.responseText;
//document.getElementById(“dop1”).innerHTML=xmlhttp.responseText;
//document.getElementById(“dop2”).innerHTML=xmlhttp.responseText;
//document.getElementById(“dop3”).innerHTML=xmlhttp.responseText;
}
}
str++;
open(“GET”、“getquestion.php?idd=“+str,true”);
xmlhttp.send();
}
下一个
美元问题
getquestion.php
<?php
$idd = intval($_GET['idd']);
require_once('db_con.php');
//$sq="select id from questions";
//$q_id = (isset($_GET['id']) ? intval($_GET['id']) : 1);
//static $r=1;
//static $q_id=0;
//$q_id++;
$sql="SELECT * FROM questions where id= '".$idd."'";
//$sql="SELECT * FROM questions WHERE id =$r";
$result = mysql_query($sql);
/*
echo "<table>
<tr>
<th>Question</th>
<th>Option1</th>
<th>Option2</th>
<th>Option3</th>
<th>Next</th>
</tr>";
*/
while($row = mysql_fetch_array($result)) {
// echo "<tr>";
echo "<h2>";
echo $row['question'];
echo "</h2>" ;
// echo "<br>" . $row['option1'] ;
// echo "<td>" . $row['option2'] . "</td>";
// echo "<td colspan='2'>" . $row['option3'] . "</td>";
// echo "</tr>";
}
//echo "</table>";
mysql_close($conn);
?>
</body>
</html>
您需要对响应进行部分解析,然后将值分配给特定的div。例如,如果您的xmlhttp.responseText`如下所示:
{
"question": "Who is it?",
"answers": ["Merry", "Billy", "Joe"]
}
function buildQuestionaree(data) {
//
// if data is a string
// data = JSON.parse(data);
//
var question = data.question;
var answers = data.answers;
document.getElementById("div1").innerHTML = question;
document.getElementById("dop1").innerHTML = answers[0];
document.getElementById("dop2").innerHTML = answers[1];
document.getElementById("dop3").innerHTML = answers[2];
}
您将看到以下内容:
{
"question": "Who is it?",
"answers": ["Merry", "Billy", "Joe"]
}
function buildQuestionaree(data) {
//
// if data is a string
// data = JSON.parse(data);
//
var question = data.question;
var answers = data.answers;
document.getElementById("div1").innerHTML = question;
document.getElementById("dop1").innerHTML = answers[0];
document.getElementById("dop2").innerHTML = answers[1];
document.getElementById("dop3").innerHTML = answers[2];
}
我们需要更多的信息。例如,responseText看起来像什么?问题是什么?看,也许吧。我想在多个div中显示一行中的结果。