[英]How to send an javascript array to a php file and compare it to the database?
I have made an online examination portal where different question are being loaded on the page from database using ajax and php when a button is clicked.The array is collecting the radio option value selected by the user i cant send the array to the php file ?Please Help. 我已经建立了一个在线考试门户网站,单击按钮时,使用ajax和php从数据库中将不同的问题加载到页面上。数组正在收集用户选择的单选选项值,我无法将数组发送到php文件吗?请帮忙。
The HTML code:- HTML代码:-
Question Palette: 问题面板:
<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>
Here is the javascript code:- 这是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);
}
});
}
Here is the check.php code:- 这是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;
?>
error:- 错误:-
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
It is obvious in your code that the "myarray" variable You are trying to get with PHP using $arr =$_GET["myarray"] was not defined in your JavaScript, Neither was it sent to the server, so PHP has nothing to retrieve. 很明显,在您的代码中,您试图在$ arr = $ _ GET [“ myarray”]中使用PHP尝试获取的“ myarray”变量未在JavaScript中定义,也未发送至服务器,因此PHP不需要执行任何操作。检索。
In your code, you seem to be storing the array with var arr. 在您的代码中,您似乎将数组与var arr存储在一起。 You should change
你应该改变
url="http://localhost/assignments/load-questions.php?qno="+quesno; URL = “HTTP://localhost/assignments/load-questions.php QNO =” + quesno;
To 至
url="http://localhost/assignments/load-questions.php?qno="+quesno+"&myarray="+arr; URL = “HTTP://localhost/assignments/load-questions.php QNO =” + quesno + “&myArray的=” + ARR;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.