如何将javascript数组发送到php文件并与数据库进行比较?
我制作了一个在线考试门户,当点击一个按钮时,使用ajax和php将不同的问题从数据库加载到页面上。数组正在收集用户选择的单选选项值。我无法将数组发送到php文件?请帮助 HTML代码:- 问题板:如何将javascript数组发送到php文件并与数据库进行比较?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我制作了一个在线考试门户,当点击一个按钮时,使用ajax和php将不同的问题从数据库加载到页面上。数组正在收集用户选择的单选选项值。我无法将数组发送到php文件?请帮助 HTML代码:- 问题板: <div class="container"> <div> <button type="button" class="btn btn-info custom" onclick="updateQ
<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>
很明显,在您的代码中,“myarray”变量 您正在尝试使用 $arr=$\u GET[“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 你好,谢谢你的回答。我编辑了代码以便更好地检查,答案会保持不变吗?你能帮我查一下吗?请。我已经通过url发送了=,现在错误未定义索引myarray不存在,但错误未定义偏移量1,2,3,4,5,6,7,8的其余部分仍然存在。您似乎正在尝试访问不存在的数组索引。这就是我的建议:在PHP中,echo$array作为字符串,echo$rows作为字符串,。让我们看看结果。这肯定会揭示出很多问题的实质。
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);
}
});
}
<?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