28
<form name="myForm" id="myForm" action="test.php" method="POST">
  <p>
  <input name="test" value="test" />
  </p>
  <p>
    <input type="submit" name="submit" value="Submit" />
  </p>
</form>

    <script>

    var auto_refresh = setInterval(
    function()
    {
    submitform();
    }, 10000);

    function submitform()
    {
      alert('test');
      document.myForm.submit();
    }
    </script>

I'm having trouble trying to auto-submit a form every 10 seconds once landed on a page. The form name is myForm action="test.php". I get the 'test' message but the page doesn't submit the form.

Any solutions besides autoloading the function upon page load?

FIXED: Removed (name="submit") from the submit button and it worked smoothly.

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Crys Ex
  • 335
  • 1
  • 4
  • 9

5 Answers5

32

You need to specify a frame, a target otherwise your script will vanish on first submit!

Change document.myForm with document.forms["myForm"]:

<form name="myForm" id="myForm" target="_myFrame" action="test.php" method="POST">
    <p>
        <input name="test" value="test" />
    </p>
    <p>
        <input type="submit" value="Submit" />
    </p>
</form>

<script type="text/javascript">
    window.onload=function(){
        var auto = setTimeout(function(){ autoRefresh(); }, 100);

        function submitform(){
          alert('test');
          document.forms["myForm"].submit();
        }

        function autoRefresh(){
           clearTimeout(auto);
           auto = setTimeout(function(){ submitform(); autoRefresh(); }, 10000);
        }
    }
</script>
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
  • Modified the script, tested, it works, works better with `setTimeout` – Mihai Iorga Sep 11 '12 at 19:12
  • You need to specify a frame, a target otherwise your script will vanish on first submit! – Mihai Iorga Sep 11 '12 at 19:14
  • I don't get it. What am I doing wrong? Uploaded the file at http://wildmedia.ro/auto_submit.php, still only 'test' pops up. Form not submitted. – Crys Ex Sep 11 '12 at 19:21
  • 2
    remove `name="submit"` from `submit` button because `document.forms["myForm"].submit` sees it as an element – Mihai Iorga Sep 11 '12 at 19:35
  • The form's `id` attribute is not necessary in this example, since the form is accessed by the `name` attribute. (Unless of course you prefer to use `document.getElementById()`, in which case one should specify an `id` attribute instead of `name`.) – jlh Jan 16 '19 at 13:34
11

Try using document.getElementById("myForm") instead of document.myForm.

<script>
var auto_refresh = setInterval(function() { submitform(); }, 10000);

function submitform()
{
  alert('test');
  document.getElementById("myForm").submit();
}
</script>
jrummell
  • 42,637
  • 17
  • 112
  • 171
7

This solution worked for me:

<body onload="setTimeout(function() { document.myform.submit() }, 5000)">
   <form action=TripRecorder name="myform">
      <textarea id="result1"  name="res1" value="str1" cols="20" rows="1" ></textarea> <br> <br/>
      <textarea id="result2" name="res2" value="str2" cols="20" rows="1" ></textarea>
   </form>
</body>
Nik
  • 452
  • 5
  • 17
4

A simple solution for a delayed auto submit:

<body onload="setTimeout(function() { document.frm1.submit() }, 5000)">
   <form action="https://www.google.com" name="frm1">
      <input type="hidden" name="q" value="Hello world" />
   </form>
</body>
HNygard
  • 4,526
  • 6
  • 32
  • 40
-7

Try this,

HtmlElement head = _windowManager.ActiveBrowser.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = _windowManager.ActiveBrowser.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "window.onload = function() { document.forms[0].submit(); }";
head.AppendChild(scriptEl);
strAdditionalHeader = "";
_windowManager.ActiveBrowser.Document.InvokeScript("webBrowserControl");
abarisone
  • 3,707
  • 11
  • 35
  • 54
  • 3
    Why should anyone use this code without any explanation? Please describe what this code do in your answer. – A.L Apr 16 '15 at 09:14