0

I have made a quiz website and set a timer using javascript. The content of the form is updated and submitted when submit button isset. How do i make the button set using javascript or how can i just add some condition when the timer hits zero which can be verified in PHP if condition

This is the javascript code

<script type="text/javascript">
var total_seconds = 3*1;
var c_minutes = parseInt(total_seconds/60);
var c_seconds = parseInt(total_seconds%60);

function CheckTime(){
    document.getElementById("quiz-time-left").innerHTML = 'Time Left: ' + c_minutes + ' minutes ' + c_seconds + ' seconds ' ;

    **if(total_seconds <=0){
        document.getElementById("register_btn").readOnly = false;**
    } else {
        total_seconds = total_seconds -1;
        c_minutes = parseInt(total_seconds/60);
        c_seconds = parseInt(total_seconds%60);
        setTimeout("CheckTime()",1000);
    }
}
setTimeout("CheckTime()",1000);
</script>

This is the HTML form

<form method="post" name="quiz" action="quiz.php" class="myform" id="quiz">
<input type="submit" id="register_btn" value="Submit Answer" name="submit_ans">
        </form>

This is the PHP code

<?php
            if(isset($_POST['submit_ans'])){updating database and condition}
?>

What shall i write in the javascript if condition so that the isset condition in PHP is satisfied. If there is any other alternative for the same please help.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Mar 19 '18 at 12:00
  • 1
    You may need to do some research and learn about the AJAX concept – RiggsFolly Mar 19 '18 at 12:01
  • Replace document.getElementById("register_btn").readOnly = false; with document.forms['quiz'].submit(); –  Mar 19 '18 at 12:04
  • Thanks @RiggsFolly will keep that in mind in coming posts – Mustafa Rangwala Mar 19 '18 at 12:06
  • @jeff But that would directly submit the form, if i was to update some data after the button isset in PHP, how would i be able to do that – Mustafa Rangwala Mar 19 '18 at 12:07
  • Put the document.forms line after your current line. This will enable the button and therefore be submitted with the form. –  Mar 19 '18 at 12:10
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Rotimi Mar 19 '18 at 12:15
  • in php do something like "if(isset($_POST['submit'])){ PHP CODE } " and with javascript have it trigger the button when timer hits zero. – twinaholic Mar 19 '18 at 13:02
  • @twinaholic that is my question what is the code for the trigger. – Mustafa Rangwala Mar 19 '18 at 13:48
  • I would combine a front-end timer to trigger an ajax form submit on timeout, but you must validate all client/browser input on server-side. Create some random test keys, hold time info in session. Maybe you should tell us more on what you need to achieve. – Paun Narcis Iulian Mar 19 '18 at 16:03
  • I only know it for jquery _$( "#register_btn" ).trigger( "click" );_ – twinaholic May 16 '18 at 18:37

0 Answers0