treehouse : what would you like to learn today?
Web Design Web Development iOS Development

php form is not submit after completion of time

  • i am writing php quiz application with timer and score will generated after submition of form.but my problem is form is not submited after complection of time

    <?php
      session_start();
      $minutes = 1; // Enter minutes
      $seconds = 0; // Enter seconds
      $time_limit = ($minutes * 60) + $seconds + 1; // Convert total time into seconds
      if(!isset($_SESSION["start_time"])){$_SESSION["start_time"] = mktime(date(G),date(i),date(s),date(m),date(d),date(Y)) + $time_limit;} // Add $time_limit (total time) to start time. And store into session variable.
      ?>
    
    
    <html>
    <head>
    <style>
    #txt {
    border:2px solid red;
    font-family:verdana;
    font-size:16pt;
    font-weight:bold;
    background: #FECFC7;
    width:70px;
    text-align:center;
    color:#000000;
    }
    </style>
    </head>
    <body>
    <input id="txt" readonly>
    
    [script>
    var ct = setInterval("calculate_time()",100); // Start clock.
    function calculate_time()
    {
     var end_time = "<?php echo $_SESSION["start_time"]; ?>"; // Get end time from session variable (total time in seconds).
     var dt = new Date(); // Create date object.
     var time_stamp = dt.getTime()/1000; // Get current minutes (converted to seconds).
     var total_time = end_time - Math.round(time_stamp); // Subtract current seconds from total seconds to get seconds remaining.
     var mins = Math.floor(total_time / 60); // Extract minutes from seconds remaining.
     var secs = total_time - (mins * 60); // Extract remainder seconds if any.
     if(secs < 10){secs = "0" + secs;} // Check if seconds are less than 10 and add a 0 in front.
     document.getElementById("txt").value = mins + ":" + secs; // Display remaining minutes and seconds.
     // Check for end of time, stop clock and display message.
     if(mins <= 0)
     {
      if(secs <= 0 || mins < 0)
      {
       clearInterval(ct);
       document.getElementById("txt").value = "0:00";
       document.f1.submit();
       alert("The time is up.");
       }
      }
     }
    </script>
    <?php
      include("connection.php");
      
              $display = mysql_query("SELECT * FROM tbl_questions ORDER BY Qno ");
      
             if (!@$_POST['submit']) {
      
                  echo "<form method=post action=\" \" name=f1 id=f1>";
                  echo "";
      
                  while ($row = mysql_fetch_array($display))
            {
      
                  $id = $row["Qno"];
                  $question = $row["question"];
                  $opt1 = $row["option1"];
                  $opt2 = $row["option2"];
                  $opt3 = $row["option3"];
                  $opt4 = $row["option4"];
                  $answer = $row["answer"];
      
                  echo "
    $id&nbsp&nbsp$question"; echo "$opt1 $opt2 $opt3 $opt4"; } echo ""; echo ""; echo ""; } else if($_POST['submit']) { //echo"executed"; $score = 0; $total = mysql_num_rows($display); while ($result = mysql_fetch_array($display)) { $answer = $result["answer"]; $id = $result["Qno"]; $or=q.$id; //echo"$or"; if ($_POST[$or] == $answer) { $score++; } } echo "You scored $score out of $total"; } ?> </body> </html>
  • Do you have a live demo? Codepen?

    Does this gives you an error? [script>

    And this? document.f1.submit(); (I think it should be document.forms.f1.submit();)

  • Does this gives you an error? [script>

    That was me...I had to change < to [ so it would render correctly on the forum.

  • thank you,actually in my code have some html tags in the above code don't have any html code it is not working...

  • Oh lol @Paulie_D :P

    document.forms.f1.submit(); or live demo / codepen.

  • i am using the same code in my online quiz...but the problem is that i want take time difference in minute and second format...and that difference i want to store in database(i.e. it user starts at 15.00 min and ends at 13.00 min the the difference will be 2 min, this value i want to store in database )..........how can i do that please provide me some code for solving this problem.....?????????