如何将javascript数组发送到php文件并将其与数据库进行比较?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (13)

我已经建立了一个在线检查门户网站,当点击一个按钮时,使用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
提问于
用户回答回答于

在你的代码中显而易见的是你试图用PHP使用$ arr = $ _ GET [“myarray”]来获取的“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;

扫码关注云+社区

领取腾讯云代金券