Javascript 将单选按钮值传递给数组中的另一个PHP页面

Javascript 将单选按钮值传递给数组中的另一个PHP页面,javascript,php,html,arrays,multidimensional-array,Javascript,Php,Html,Arrays,Multidimensional Array,我正在做一个在线测试项目。我已将代码粘贴到下面。我想收集数组中所有问题的无线电值,并将其从index.php发送到checkAnswer.php。在这个文件中,我想收集Aray中所有用户选择的无线电值,以便将其与正确答案的数组进行比较。我想比较两个数组,找出正确答案和错误答案 这是json [ { "number": 1, "question": "Eighteen thousandths, written as a decimal, is:

我正在做一个在线测试项目。我已将代码粘贴到下面。我想收集数组中所有问题的无线电值,并将其从index.php发送到checkAnswer.php。在这个文件中,我想收集Aray中所有用户选择的无线电值,以便将其与正确答案的数组进行比较。我想比较两个数组,找出正确答案和错误答案

这是json

[

      {
          "number": 1,
          "question": "Eighteen thousandths, written as a decimal, is:",
          "op1" : 20,
          "op2" : 30,
          "op3" : 59,
          "op4" : 54,
          "correct_answer": "op1"
      },
      {
          "number": 2,
          "question": " thousandths, written as a decimal, is:",
          "op1" : 20,
          "op2" : 30 ,
          "op3" : 59,
          "op4" : 54,
          "correct_answer": "op3"
      },
      {
          "number": 3,
          "question": " thousandths, written as a decimal, is:",
          "op1" : 20,
          "op2" : 30 ,
          "op3" : 59,
          "op4" : 54,
          "correct_answer": "op3"
      }
  ]


这是index.php

 <?php
session_start() ;
$string = file_get_contents("example_10.json");
$json_data = json_decode($string, true);

<section class="solve">
<div class="contianer">
  <div class="row">
    <div class="col-md-8">
        <form action="checkAnswer.php" method="post">

      <?php
      foreach ($json_data as $key => $value) {
          echo
          "<p><span class='que'> Question</span>&nbsp;&nbsp". $value['number']."&nbsp;&nbsp". "<br><hr class='line'>". $value['question']."<br><br>".
          "<pre>"."<input type='radio'  name='question" . $value['number']."' value='op1'>" ." " , $value['op1']."</pre>".
          "<pre>"."<input type='radio' name='question" . $value['number']."' value='op1'>" ." " , $value['op2']."</pre>".
          "<pre>"."<input type='radio' name='question" . $value['number']."' value='op1'>"." "  , $value['op3']."</pre>".
          "<pre>"."<input type='radio' name='question" . $value['number']."' value='op1'>"." " , $value['op4']."</pre>".
          "</p>";
      }
      ?>
      <input class="submitBtn" type="submit" name="submit" value="SUBMIT">
      <button id="">click</button>
       </form>
    </div>
    <div class="col-md-4"></div>
  </div>
</div>
</section>

?>

点击
?>
这是checkANswer,我想要一个无线电值数组



我认为您不需要传递整个数组,只需传递所选的值并将其与正确答案进行比较即可。您不需要将其过于复杂。

在checkANswer.php文件中,您可以使用

您可以使用以下方法检查用户提交的数据:

echo”“;打印(邮政美元);

然后,您需要将此POST请求数据与您的答案数据进行比较。

我理解您的意图。您从JSON获取正确答案的代码对我来说很好。我还没有测试过

因此,基本上,您现在需要做的是将表单发送到PHP(您已经在对
checkAnswer.PHP
执行此操作),并通过
Get
Post
获取用户的正确答案。您必须将这些值添加到另一个数组中,然后将每个值与另一个数组进行比较


重要提示:您的订单必须在两个数组上都匹配。

但是我必须在每个问题后添加提交按钮…我想通过单击按钮发送所有这些值不,只需为表单添加一个提交按钮,并将表单中的所有输入字段与提交按钮一起放置,这样当您提交时,您将获得所有内容创建一个form,将您的字段和单选按钮放在其中,然后将您的checkanswer.php放在操作中,根据您的表单方法,您可以使用$\u GET或$\u POST获取提交的表单数据。什么不起作用?您能告诉我如何在正文中回显该数组。因为它当前以注释形式显示在html标记上方。我将如何访问它ValueYou谈论的是用户提交的POST数据吗?是的..现在已经解决了。假设有人未填写任何问题,那么如何在POST中传递0值?如果(空($_POST['question']){$ans=0}

<?php

$json_file = file_get_contents("example_10.json");
$json_decode = json_decode($json_file, true);
print_r ($json_decode);

$answer_array= array();

foreach ($json_decode as $key => $value) {
      array_push($answer_array,$value['correct_answer']);
}

print_r($answer_array)
$lengthOfJsonQuestion = count($answer_array);


 ?>


echo "<pre>";print_r($_POST);