简体   繁体   中英

Auto Submit Php Form with JAvascript

I know this question have already answered but it didn't worked for me. I am creating a mcq question page. In which question have been generating by Json file. I am setting time limit of 90 minutes to the solving the question after 90 minutes the form must auto submit form to the the action="page" of the the form. I tried using set time out but it failed and i am also not getting any error.

This is Form in HTM AND PHP

  <div class="col-md-8">
     echo   <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>".
          "<pre>"."<input type='radio' name='question" . $value['number']."' value='0' required>"."LEAVE QUESTION"."</pre>".

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

This is Js

    setTimeout(function(){
        $('#gi').submit();
    },10000);
</script>

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;

?>

Try this function

window.setTimeout(function() {
 document.forms['mockForm'].submit();
 }, 10000 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM