我已经做了一个在线考试门户网站,其中不同的问题是从数据库加载页面使用ajax和php,当一个按钮是clicked.The数组正在收集由用户选择的单选项值我不能发送该数组到php文件?请帮助。
HTML代码:
问题调色板:
<div class="container">
<div>
<button type="button" class="btn btn-info custom" onclick="updateQuestion(1)" style="margin-right:16px">1</button>
<button type="button" class="btn btn-info custom" onclick="updateQuestion(2)" style="margin-right:16px">2</button>
<button type="button" class="btn btn-info custom" onclick="updateQuestion(3)" style="margin-right:16px">3</button>
</div><br>
<div>
<button type="button" class="btn btn-info custom" onclick="updateQuestion(4)" style="margin-right:16px">4</button>
<button type="button" class="btn btn-info custom" onclick="updateQuestion(5)" style="margin-right:16px">5</button>
<button type="button" class="btn btn-info custom" onclick="updateQuestion(6)" style="margin-right:16px">6</button>
</div><br>
<div>
<button type="button" class="btn btn-info custom" onclick="updateQuestion(7)" style="margin-right:16px">7</button>
<button type="button" class="btn btn-info custom" onclick="updateQuestion(8)" style="margin-right:16px">8</button>
<button type="button" class="btn btn-info custom" onclick="updateQuestion(9)" style="margin-right:16px">9</button>
</div><br>
<button type="button" class="btn btn-info btn-md" id="mark" onclick="loadNextQues(1)">Mark for Review & Next</button>
<button type="button" class="btn btn-info btn-md">Clear Response</button>
<button type="submit" class="btn btn-info btn-md" id="save" style="position:absolute;right:20px;bottom:35px" onclick="loadNextQues(0)">Save and Next</button>
<form action="check.php">
<button type='submit' class="btn btn-info btn-md" id="water" style="visibility:hidden;position:absolute;right:20px;bottom:35px" onclick="Checkanswers()">submit</button>
</div></div><br>
以下是javascript代码:
var arr=new Array();
function loadNextQues(flag){
quesno++;
var Selected = document.querySelectorAll('[name="op"]:checked');
if (Selected != null) {
Selected.forEach(function(radio) {
arr.push(radio.value);
});
}
else{
arr.push(0);
}
console.log(arr);
if(flag==1){
//add css to the button for mark for review
}
else{
//add css for the button as answered
}
url="http://localhost/assignments/load-questions.php?qno="+quesno;
$.get(url,function(data,status){
response=JSON.parse(data);
console.log(response);
var quest="<p>"+response.qno+" "+response.question+"</p>";
quest+="<form>";
quest+="<input type=\"radio\" name=\"op\" value='"+response.opA +"'>A."+response.opA+"<br>";
quest+="<input type=\"radio\" name=\"op\" value='"+response.opB +"'>B."+response.opB+"<br>";
quest+="<input type=\"radio\" name=\"op\" value='"+response.opC +"'>C."+response.opC+"<br>";
quest+="<input type=\"radio\" name=\"op\" value='"+response.opD +"'>D."+response.opD+"<br>";
quest+="</form>";
document.getElementById("questBox").innerHTML=quest;
});
}
function Checkanswers(){
var Selected = document.querySelectorAll('[name="op"]:checked');
if (Selected != null) {
Selected.forEach(function(radio) {
arr.push(radio.value);
});
}
else{
arr.push(0);
}
alert(JSON.stringify(arr));
$.get({
url:"check.php",
data: {myarray : JSON.stringify(arr)},
success: function(data)
{
alert(data);
}
});
}
以下是check.php代码:
<?php
session_start();
require 'includes/dbh.inc.php';
$array = array();
$arr =$_GET["myarray"];
$arr1=substr($arr,1,strlen($arr)-1);
$array=explode(',',$arr1);
$result = 0;
$i= 1;
$sql = " SELECT * FROM questionpaper ";
$query = mysqli_query($conn,$sql);
while($rows = mysqli_fetch_array($query)){
$checked = ($rows['ans'] == (int)$array[$i]) ;
if($checked){
$result++;
}
$i++;
}
echo "<br> Your total score is".$result;
?>
错误:-
Notice: Undefined index: myarray in D:\XAMPP\htdocs\assignments\check.php on line 8
Notice: Undefined offset: 1 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 2 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 3 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 4 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 5 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 6 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 7 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 8 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 9 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 10 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 11 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 12 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 13 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 14 in D:\XAMPP\htdocs\assignments\check.php on line 20
Notice: Undefined offset: 15 in D:\XAMPP\htdocs\assignments\check.php on line 20
发布于 2019-04-14 23:31:07
很明显,在您的代码中,您试图使用$arr =$_GET"myarray“获取的"myarray”变量并没有在您的JavaScript中定义,也没有发送到服务器,所以PHP没有什么可检索的。
在您的代码中,您似乎使用var arr来存储数组。你应该改变
url="http://localhost/assignments/load-questions.php?qno="+quesno;
至
url="http://localhost/assignments/load-questions.php?qno="+quesno+"&myarray="+arr;
https://stackoverflow.com/questions/55675340
复制相似问题