我有一个测验管理系统,需要从数据库中获取数据并在点击下一个按钮时逐个显示数据。
我希望使用Ajax在使用文件读写功能单击下一个按钮后将数据插入数据库。
$sql=mysqli_query($con,"select * from tbl_question where setid='$set' and status=1 ORDER BY RAND()");
while($sha=mysqli_fetch_array($sql)) {
<h4><? echo $sha['id'];?></h4>
我想通过Ajax提交问题。请建议我解决。
答案 0 :(得分:0)
我正在使用此response创建三个问题,这些问题出现在单击按钮上。
我希望答案是自我解释的。如果没有,请告诉我。我用过香草js。欢呼声。
window.onload = getRequest();
let response;
let allFirstNames;
let questionDiv = document.getElementById("question");
function getRequest() {
let xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4) {
response = xmlhttp.responseText;
parseResponse()
}
}
xmlhttp.open("GET", "https://reqres.in/api/users?page=2", true);
xmlhttp.send();
}
function parseResponse() {
allFirstNames = JSON.parse(response)["data"].map((dataObj) => {
return dataObj["first_name"];
});
}
document.getElementById("printQ").onclick = printQuestions;
function printQuestions() {
clearCurrentQuestions();
if (allFirstNames[0] != undefined) {
let question = document.createElement("p");
question.innerHTML = "Who is "+ allFirstNames[0] + " ?";
document.getElementById("question").appendChild(question);
allFirstNames.splice(0,1);
}
else {
let question = document.createElement("p");
question.innerHTML = "No More Questions. End of Quiz";
document.getElementById("question").appendChild(question);
}
}
function clearCurrentQuestions() {
let node = document.getElementById("question")
while(node.hasChildNodes()) {
node.removeChild(node.lastChild);
}
}
&#13;
<button id="printQ">
Print Next Question
</button>
<div id="question">
<p>
Hi, Welcome to the Quiz, Click above button for Qns
</p>
</div>
&#13;