0

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;

?>

9
  • 1
    Just remove name attribute <input class="submitBtn" type="submit" value="SUBMIT"> Commented Nov 21, 2019 at 5:03
  • 1
    FYI, with that solution, the user can refresh the page and the timer will reset. Not a great solution. Commented Nov 21, 2019 at 5:04
  • well the timer will be reset yes, but also the form so is that really a problem? Commented Nov 21, 2019 at 5:06
  • Does this answer your question stackoverflow.com/questions/1961326/… Commented Nov 21, 2019 at 5:06
  • 1
    yes ... @epascarello . And in next page i have an isset function checking for isset($_POST['submit']). If i remove name this will blunder my next page Commented Nov 21, 2019 at 5:06

1 Answer 1

2

Try this function

window.setTimeout(function() {
 document.forms['mockForm'].submit();
 }, 10000 );
Sign up to request clarification or add additional context in comments.

4 Comments

Got this error TypeError: document.forms.mockForm.submit is not a function
change submit button name to btnsubmit because if button named submit it will override to submit function
In form or in JS ?
<input class="submitBtn" type="submit" name="btnsubmit" value="SUBMIT">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.