SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    347
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How should I automatically re-submit a form ?

    I have created a small PHP App that extracts stuff from a MySQL database. Five records are selected at random for extraction and showing on screen. At the bottom of the display is a form (id = 'commentsform') with 'submit' button, which will re-run the script and display a new random selection of results. Works perfectly when I click the submit button manually.

    I've written two alternative ways of doing it, which aren't relevant here. I want to see which way runs fastest. I've set up a timer on the script, which works fine, but the result of a single run is so fast I'm trying to run the script 100 times to get a better figure. So at the bottom of my script I have:
    Code:
    <script type='text/javascript'>
    	function goAgain() {
    //		var frm = document.getElementById('commentsform');
    //		frm.submit();
    		document.forms[0].submit();
    	}
    	window.onload = goAgain;
    </script>
    </body>
    </html>
    As you can see, I've tried two ways of accessing the form (one is commented out). In BOTH cases I'm getting a Javascript console error message "document.forms[0].submit is not a function" (or "frm.submit is not a function").
    This approach to automatically submitting a form is widely advocated on the internet, so I can't see why it doesn't work for me. Can anyone help here, please ?

    I'm aware that if it did work there is presently no way of stopping the script, but I can probably take care of that once it works. (The PHP counts to 100, but not not the JS. The easy way might be not to display the form after 100 cycles.)
    Tim Dawson
    Isle of Mull, Scotland

  2. #2
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    347
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Obviously this isn't an every-day requirement !

    It's still unclear to me why an automatic 'submit' on the form doesn't work. The solution is to use ''click' on the submit button:
    Code:
    function goAgain() {
    		var btn = document.getElementById('go');
    //		alert ("Button is " + btn.id);
    		btn.click();
    	}
    the function 'goAgain' is called after the form has loaded, and the form is now re-submitted as soon as it loads. All that remains is to link the PHP counting to the JS so that it will stop at some pre-determined number of cycles. The PHP counting uses a SESSION to preserve the count.
    Tim Dawson
    Isle of Mull, Scotland


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •