Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2004
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Stopping form submit reload

    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.

    Code:
    <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?

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    How is the searching done? Do you use a serverside language or javascript kind of like a find in page type thing?

  • #3
    New Coder
    Join Date
    Jan 2004
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    function netSearch(formname)
    {
    TEXT=formname.TEXT.value;
    plus="";
    
    for(var i=0; i<TEXT.length; i++)
    {
    	if(TEXT.charAt(i)==" ") plus+="%20";
    	else plus+=TEXT.charAt(i);
    }
    
    TEXT=plus;
    if(formname.google.checked) newWindow=window.open("http://www.google.com/search?hl=en&q="+TEXT);
    }

  • #4
    New Coder
    Join Date
    Jan 2004
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)...

  • #6
    New Coder
    Join Date
    Jan 2004
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a lot, the third suggestion works great; I didn't realize just adding "return false" would fix the problem so I appreciate it.

  • #7
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

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