Hi all I have a very simple code to submit a form onLoad of a page, it works fine but IT keeps on refreshing every second, why ??? I really don't understand. JS function myfunc () { var form = document.getElementById("foo"); form.submit(); } Code (markup): HTML <body onload="myfunc()"> <form id="foo" action="EventData" method="post" ></form> Code (markup): Your help is much appreciated. Zed
I don't know javascript very well but I think this is what is happening ... ... the HTML load causes myfunc() to run ... myfunc() does a form.submit so the HTML starts up again, causing myfunc() to run again... I could be wrong.
It refreshes because the browser reads: //(page load so call command) 1. onload() call function //(getElementById = get the forms name 2. function get form name //(and submit form to server) 3. submit form //(submiting a form cause the page to refresh so... go back to page load!) looks like you made an infinite loop. If you want it to stop refreshing try deleting the onload()
You shouldn't be calling the myfunc() on the onload, but when the user has entered the details and pressed a button (onclick). The form.submit(); will refresh the page.
Thanks for your replies, What I am trying to do is , I have a index.jsp page that has to show some data from database, because it's the first page customer views it has to be onLoad so in order to fetch this data I'm trying to trigger a hidden form. All works well apart from like I said before, keeps on refreshing and refreshing and refreshing ... Zed
Hmm... from what I'm understanding you want to display data from a database without refreshing the page. You should use AJAX.
when the form is submitted and your data is loaded, Use your server-side code to add a hidden input box onto the end of the data, or whereever. e.g (html loaded from server-side code): ...database data.... <input id="dataLoaded" type="hidden" /> Code (markup): then change your code... function myfunc () { var form = document.getElementById("foo"); if(!document.getElementById("dataLoaded")) form.submit(); } Code (markup): The reason why it keeps refreshing, because when the form submits, the page is reloaded, therefore your code is executed again and the form is submitted again. Forming an endless loop.