View Full Version : Stopping form submit reload

10-20-2005, 11:17 AM
My site has a simple search engine but every time I search for something, the page reloads and it really does not need to.

Here is the html part which I think is the place of error, let me know if you need to see the javascript too.

<INPUT TYPE="text" SIZE="20" NAME="TEXT" onClick="form.TEXT.select()">
<INPUT TYPE="submit" VALUE="search"><BR>
<INPUT TYPE="checkbox" NAME="google">google<BR>
<INPUT TYPE="checkbox" NAME="yahoo">yahoo
Any tips on how to stop the page from reloading after submitting the form so I can search again?

10-20-2005, 11:20 AM
How is the searching done? Do you use a serverside language or javascript kind of like a find in page type thing?

10-20-2005, 08:38 PM
Javascript; here's the gist of it. There is more code, but I tried to distill it down to what happens and not include things like exceptions for not selecting a checkbox or not including any text because I doubt they affect the problem.

function netSearch(formname)

for(var i=0; i<TEXT.length; i++)
if(TEXT.charAt(i)==" ") plus+="%20";
else plus+=TEXT.charAt(i);

if(formname.google.checked) newWindow=window.open("http://www.google.com/search?hl=en&q="+TEXT);

10-21-2005, 06:08 AM
I don't know a lot about this, but is there a simple solution by adding an "onSubmit" to the form and using it to stop the page from reloading so one can search again?

10-21-2005, 02:42 PM
You could try:
* In you <form>, onSubmit="searchFunction(); return false;"
* Or in your <input type='checkbox'>, onClick="searchFunction()"
* Or in your <input type='submit'>, onClick="searchFunction(); return false;"

where searchFunction() is your custom searching function. In the first and last cases, you need to include the "return false" to prevent the form from actually submitting (and hense reloading)...

10-21-2005, 07:08 PM
Thanks a lot, the third suggestion works great; I didn't realize just adding "return false" would fix the problem so I appreciate it.

10-21-2005, 07:41 PM
Also, if you have some type of validation function, you could have that return true if you want the form to be submitted or false if you want to halt submittion. Then just onClick="return validationFunction()" in your submit button.