我在验证和显示给定答案是否正确时遇到问题。代码应检查结果并显示绿色 div,答案正确时隐藏,答案错误时应显示红色 div。
这是我的ajax代码:
function x() {
var rads = document.getElementsByName("rads");
for ( var i = 0; i < rads.length; i++ ) {
if ( rads[i].checked ){
var val = rads[i].value;
return val;
}
}
}
function post_answer(){
var p = new XMLHttpRequest();
var id = document.getElementById('qid').value;
var url = "userAnswers.php";
var vars = "qid="+id+"&radio="+x();
p.open("POST", url, true);
p.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
p.onreadystatechange = function() {
if(p.readyState == 4 && p.status == 200) {
document.getElementById("status").innerHTML = '';
var url = 'quiz.php?question=<?php echo $next; ?>';
window.location = url;
}
}
p.send(vars);
document.getElementById("status")
}
php代码:
session_start();
if(isset($_POST['radio']) && $_POST['radio'] != ""){
$answer = preg_replace('/[^0-9]/', "", $_POST['radio']);
if(!isset($_SESSION['answer_array']) || count($_SESSION['answer_array']) < 1){
$_SESSION['answer_array'] = array($answer);
}else{
array_push($_SESSION['answer_array'], $answer);
}
}
if(isset($_POST['qid']) && $_POST['qid'] != ""){
$qid = preg_replace('/[^0-9]/', "", $_POST['qid']);
if(!isset($_SESSION['qid_array']) || count($_SESSION['qid_array']) < 1){
$_SESSION['qid_array'] = array($qid);
}else{
array_push($_SESSION['qid_array'], $qid);
}
$_SESSION['lastQuestion'] = $qid;
}
问题是我无法显示答案是否正确。
它应该显示 .$answer。是不正确的。请提前帮我thanx