0

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

1 Answers1

-1

change this

var arr = [];
 var Selected = document.querySelector('[name="op"]:checked');
 if (Selected!= null) {
 arr.push(Selected.value);
 } 
console.log(arr);

change the php to this i assume answer is in same order as you are sending in the array.

   $count = 0;
   $sql = " SELECT * FROM questionpaper ";
   $query = mysqli_query($conn,$sql);
   $i=0;
   while($row=mysqli_fetch_array($query)){
   if ($array1[$i]==$row['answer']) {
    $count++;
    $i++;

    }
   echo $count;

if you can show how db structure is it will be easy to give you exact answer.

bhaskarkh
  • 179
  • 2
  • 11
  • This doesn't work,I have changed the above code and still it doesn't work. – kaunish roy Apr 14 '19 at 18:02
  • Are you getting any error or not getting the desired results. Try to print the value you are getting from db and cross verify manual – bhaskarkh Apr 14 '19 at 18:07
  • I have changed the above code to show you the error and edited the database portion to show you the structure of it. The desired results i should be getting is that the $array from the javascript file which is being sent to the php file should be compare to the $rows['ans'] and if they match the $result would increment which will in the end give the score. – kaunish roy Apr 14 '19 at 18:18
  • Use php's in built isset() function to check whether the variable is defied or not. check this link https://stackoverflow.com/questions/26670444/how-to-solve-notice-undefined-index-id-in-c-xampp-htdocs-invmgt-manufactured – bhaskarkh Apr 14 '19 at 18:22
  • `var arr = [] var Selected = document.querySelector('[name="op"]:checked'); if (Selected!= null) { arr.push(Selected.value) } console.log(arr);` and check in console(in chrome) to verify whether you are getting right value of radio button. – bhaskarkh Apr 14 '19 at 19:03
  • yes i am getting the right value,only the "myJSarray" is not getting to the php file. Do you have any solution how to solve this. please help. – kaunish roy Apr 14 '19 at 20:52
  • I have given the isset() function and also send it by url:"http//:assignments/check.php/myarray="+arr;. Now error is undefined offset 1 in check.php.....undefined offset 2 in ......indefined offset 3 in.... – kaunish roy Apr 15 '19 at 09:27