I have made an online exam portal where different radio options selected by the users are being stored in an array, then i am sending that array in a php file to get checked with the database,and this is where i am facing the problem.The answers are in the database and after i fetch them i compare them to the array but it is not getting solved. please help .
This is the table questionpaper from which answers are coming from:-
qid questions opa opb opc opd ans
1 what is that? car dog john star 4
2 . what is that? car dog john star 4
3 what is that? car dog john star 4
4 what is that? car dog john star 4
5 what is that? car dog john star 4
6 what is that? car dog john star 4
Here is the javascript code from where i am send it:-
function Checkanswers(){
var Selected = document.querySelectorAll('[name="op"]:checked');
if (Selected != null) {
Selected.forEach(function(radio) {
arr.push(radio.value);
});
}
alert(JSON.stringify(arr));
$.ajax({
type: "POST",
url:"check.php",
data: {"myJSarray" : JSON.stringify(arr)},
success: function(data)
{
alert(data);
}
});
}
Here is the php code which is doing all of it:-
<?php
session_start();
require 'includes/dbh.inc.php';
$array = array();
$arr =$_POST["myJSarray"];
$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
Your total score is0