我做了一个在线考试门户,当点击一个按钮时,用ajax和php从数据库页面加载不同的问题,数组正在收集用户选择的单选选项值,我不能把数组发送到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

最佳答案

很明显,在您的代码中,“myarray”变量
您正在尝试使用PHP
$arr=$_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;

关于javascript - 如何将JavaScript数组发送到php文件并将其与数据库进行比较?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55675340/

10-11 12:58