1

Here is i want to happened to my HTML code.. i have an html code that is written in a php code and i want to add a java script(or anything that can help me) to automatically submit my form after 10 seconds.. I am creating an examination system for my school and i want it to be time bounded.. this is the code..

 echo "<form action=\"PostTest1.php\" method=\"Post\">";
    echo "<tr><td>";
      echo $row['Description'] . "<br>";
      echo "<input type=\"radio\" name=\"Answer\" value=\"A\"> A.)" . $row['Ans1'];
      echo "<br>";
      echo "<input type=\"radio\" name=\"Answer\" value=\"B\"> B.)" . $row['Ans2'];
      echo "<br>";
      echo "<input type=\"radio\" name=\"Answer\" value=\"C\"> C.)" . $row['Ans3'];
      echo "<br>";
      echo "<input type=\"radio\" name=\"Answer\" value=\"D\"> D.)"  . $row['Ans4'];
      echo "<br>";
      echo "<input type=\"Submit\" value=\"Submit\" name=\"submit\">";
    echo "</td></tr>";
echo "</form>"; 

i wonder what language you can help me.. its either JS or JQ.. thankyou very much! :)

AllenTheGreat
  • 39
  • 1
  • 1
  • 5
  • see http://stackoverflow.com/questions/4148210/how-do-i-submit-a-form-in-javascript-with-a-delay or http://stackoverflow.com/questions/9947515/submit-a-form-with-jquery-after-delay or http://stackoverflow.com/questions/15058562/submit-hidden-form-after-a-delay-or-immediately-if-a-link-is-clicked – Sean Sep 04 '13 at 05:28

3 Answers3

4

using jquery setTimeout()

<script type="text/javascript">
 $(function(){  // document.ready function...
   setTimeout(function(){
      $('form').submit();
    },10000);
});
</script>

add this inside you head tag <head> .. better you add id to your form and use id selector $('#formID').submit();

updated

using form id to be specific..

echo "<form action=\"PostTest1.php\" method=\"Post\" id='formID'>";
 .....

and

setTimeout(function(){
      $('#formID').submit();
    },10000);
bipen
  • 36,319
  • 9
  • 49
  • 62
  • sorry for delay... updated my answer.. check it out...using `$('form')` ..this will submit all the form present in a document.. so just to be spefific.. you can id add to you form and use id selector like i mentioned – bipen Sep 04 '13 at 06:01
  • Sir i already tried your code.. adding a `echo "
    ";` and adding a Script under my form like this-> `echo "";` i wonder it's not auto submitting..
    – AllenTheGreat Sep 04 '13 at 06:06
  • i tried it also in the head tag.. and it's still not working.. :( – AllenTheGreat Sep 04 '13 at 06:10
  • are you getting anyerrors.. check your console... you can add an alert inside the setimeout function just to check if it is called or not – bipen Sep 04 '13 at 06:13
  • i'm not getting any errors.. it's just not auto submitting.. i already changed the time to 1000 to be 1 second but it's still not auto submitting – AllenTheGreat Sep 04 '13 at 06:19
  • just asking... did u load jquery file ?? here is working fiddle http://jsfiddle.net/kFbLY/ – bipen Sep 04 '13 at 06:24
  • I think it hits me.. Jqueries are not suitable for php files.. i dont know if im right but maybe that is the case why it is not running.. what do you think sir? – AllenTheGreat Sep 04 '13 at 06:54
  • no that won't matter... you can add javascript/jquery script in your php file ....but just need to make sure the cides is inside the `` tag. did you try adding alert inside setTimeout... there has to be something that is breaking the code...go to chrome, press F12, and click to console tab.. refresh the page... if any error it will show there.. try it out – bipen Sep 04 '13 at 07:03
  • now its working.. and the alert is working i included in the head tag a `` thats why the alert now is showing but the problem is.. the form is still not auto submitting.. i wonder why.. – AllenTheGreat Sep 04 '13 at 07:15
1

Try this

<head>
...
<script type="text/javascript">
window.onload(function(){setTimeout( "document.forms[0].submit();", 10000 )});
...
</script>
...
</head>

Only just noticed your question ... sorry! The code snipped can go at any place in the file since it will only be fired 10 seconds after the page is loaded completely. I put it into the <head> section, probably the 'standard location' for JavaScript.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0

you can use

setTimeout(function() {   //calls click event after a certain time
 $('form').submit();
}, 10000);

It is better to change input type from submit to button

rove101
  • 11
  • 2