在POST数组中传递默认值

时间:2019-11-21 06:48:38

标签: javascript php arrays json ajax

我有一个MCQ测试问题页面。问题和选项按foreach循环打印,并以JSON格式存储。当用户提交时,我有一个POST数组,其中存储了单击特定问题的单选按钮。我已经全部工作了,但是其中有一个错误,即如果用户留下任何问题,则帖子数组会因一些未知的索引/值值而受到干扰。正确的答案数组中包含所有索引。所以我不能比较两个数组。如果没有索引/键,如何将0设置为数组中的值。

HTML FORN

 <form id="gi" method="post" name="mockForm" action="checkAnswer.php">

      <?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' required>" ." " , $value['op1']."</pre>".
          "<pre>"."<input type='radio' name='question" . $value['number']."' value='op2' required>" ." " , $value['op2']."</pre>".
          "<pre>"."<input type='radio' name='question" . $value['number']."' value='op3' required>"." "  , $value['op3']."</pre>".
          "<pre>"."<input type='radio' name='question" . $value['number']."' value='op4' required>"." " , $value['op4']."</pre>"

              "</p>";
      }
      ?>
            <input class="submitBtn" type="submit" name="btnsubmit" value="SUBMIT">
            <button onclick="hit(); handleClick();">click</button>
       </form>

PHP CHECKANSWER.php

<?php
$contentOfJsonFile = file_get_contents("example_10.json");
$JsonData = json_decode($contentOfJsonFile, true);

$correctAnswerArray = [];

$wrongCount = 0;
$correctCount = 0;
foreach ($JsonData as $key => $value) {
    array_push($correctAnswerArray, $value['correct_answer']);
}

    $userAnswerArray = $_POST;

$breakForeach = count($userAnswerArray) - 1;
$loopRun = 0;
foreach ($userAnswerArray as $key => $answer) {


    $questionNumber = substr($key, -1);

    if ($correctAnswerArray[$questionNumber] === $answer) {
        # increment correct
        $correctCount++;
    } else if($correctAnswerArray[$questionNumber] === 0) {
        # increment wrong
        $wrongCount++;
    }
    else {
        $wrongCount++;
    }


$loopRun++;

    if($breakForeach == $loopRun ) break;
}
echo "<br>".$correctCount."<br>";
echo $wrongCount;

?>

1 个答案:

答案 0 :(得分:1)

叹息...请尝试从中学到一些东西。

<form id="gi" method="post" action="checkAnswer.php">
  <?php foreach ($json_data as $value): ?>
    <p>
        <span class="que">Question</span>
        <?php echo $value["number"]; ?>
    </p>

    <hr class="line">

    <?php 
        for ($i = 1, $i <= 4; $i++) {
            echo sprintf('<input type="radio" name="question[%d] value="%s" required> %s', 
                $value["number"], 
                $value["op{$i}"], 
                $value["op{$i}"]);
        }
    ?>
  <?php endforeach; ?>
<?php
// FIXME Database
$answerKey = json_decode(file_get_contents("example_10.json"), true);

$score = 0;
foreach ($_POST as $i => $answer) {
    if (isset($answerKey[$i]) && $answerKey[$i] == $answer) {
        // increment correct
        $score++;
    }
}

echo "<br>{$score}<br>";
echo count($answerKey) - $score;