#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2

    Set time limit exam


    Hi,

    I am trying to write a math quiz program in php. I want to set 30 seconds for each question. How can i disable score button after 60 seconds for 2 questions that i have?

    PHP Code:
    <?php
    set_time_limit
    (60);
    echo 
    " <form name=\"form1\" method=\"post\" action=\"sessionS1.php\">";
    echo 
    "<table width='600' cellpadding='5' cellspacing='5' border='1'>";

    echo 
    "<tr> <td> Q1 </td> <td>  2  *  2    </td> <td> <input type = 'text' size = '2' name = one > </td> </tr>";
    echo 
    "<tr> <td> Q1 </td> <td>  2  *  8    </td> <td> <input type = 'text' size = '2' name = one > </td> </tr>";


    // how do i write IF ELSE statement here to disable?

    echo "<tr><td colspan=6 align='right'><input type='submit' value='Score' name='btn_score'></td></tr>";


    echo 
    "</table>";
    echo 
    " </form> " ;
    ?>
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,128
    Rep Power
    9398
    Using Javascript. Are you using any frameworks like jQuery or YUI?

    ...Probably not. window.setTimeout will execute a function after however many milliseconds you want.
    Code:
    window.setTimeout(function() {
        // ...
    }, 60 * 1000);
    In that function you need to disable the button.
    Code:
    document.form1.btn_score.disabled = true;
    But because that's all client-side you still need to have your PHP make sure that the form isn't accepted if the user managed to re-enable the button and click it (or simply submitted the form).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2
    Thanks for your help!!

    I have the below. I am actually lost. I am not getting any error but it is not doing anything. I am new. If you could help?

    PHP Code:
    <html>
    <
    body>



    window.setTimeout(function() {
     <
    form name=\"form1\" method=\"post\" action=\"sessionS1.php\">
    <table width='600' cellpadding='5' cellspacing='5' border='1'>

    <tr> <td> Q1 </td> <td>  2  *  2    </td> <td> <input type = 'text' size = '2' name = one > </td> </tr>
    <tr> <td> Q1 </td> <td>  2  *  8    </td> <td> <input type = 'text' size = '2' name = one > </td> </tr>
    <tr><td colspan=6 align='right'><input type='submit' value='Score' name='btn_score'></td></tr>

    </table>
     </form> 
    document.form1.btn_score.disabled = true;
    }, 3000 );


    </body>
    </html> 
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,128
    Rep Power
    9398
    Actually you are getting errors, you're just not seeing them.

    What I gave you was Javascript code, not PHP code. You have to make sure that your PHP outputs it just like it outputs the quiz form.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2
    I have tried so much. I could not solve. I tried many different ways. If someone could help me with the coding, it would be awesome.

    Thanks.
  10. #6
  11. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,128
    Rep Power
    9398
    Try writing it without any PHP. None at all. Purely HTML and Javascript.
    It's two separate chunks of code. One is the HTML you had before, one is the Javascript I gave you. Just put them both in the same file, preferably with the <script> at the very end of the <body>'s content.

IMN logo majestic logo threadwatch logo seochat tools logo