Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

No Submit on Error

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
I have an HTML search form with a JavaScript alert but when there is an alert, the form is still submitted.&nbsp;&nbsp;How can I make it stay on the HTML page and not actually submit the form?<br><br>Here is the alert:<br><br>function OnSubmitKw(form){<br>if (form.Keywords.value==&quot;&quot; && form.searchtype.value==&quot;Keyword&quot;){<br>&nbsp;&nbsp;&nbsp;alert(&quot;You did not enter any Keywords! Please try again.&quot;);}<br>}
 
do not use a submit button, just a simple button with onclick=&quot;whatever(this.form)&quot;<br><br>then in the function whatever(f) {<br>isOK=....<br>If (isOK) {<br>&nbsp;&nbsp;f.submit();<br>} <br>else {<br>&nbsp;alert(....<br><br>of course there should be no onsubmit event in the form tags
 
Well, I tried it but can't seem to get it to work (I'm not a JavaScript programmer). It might be because I need to say &quot;if the form is NOT correct, then . . &quot; and I don't think I have it quite right.&nbsp;&nbsp;Here is what I have now:<br><br>function OnSubmitKw(form) {<br>if (form.searchtype.value==&quot;Keyword&quot; && form.keywords.value==&quot;&quot;);<br>{<br>alert(&quot;You did not enter any Keywords! Please try again.&quot;);<br>}<br>else {<br>&nbsp;&nbsp;form.submit();<br>&nbsp;&nbsp;}<br><br>In the search HTML form I have:<br>&lt;input type=&quot;button&quot; value=&quot;Begin Search&quot; onclick=&quot;OnSubmitKw(this.form)&quot;&gt;<br><br>If there IS a keyword entered and IF I press enter, the form is submitted, but if there is no keyword and/or if I press the Begin Search button, there is a generic JavaScript error.<br><br>Thanks!<br><br>DonP
 
Dear DonP,<br><br>I could identify 2 possible sources:<br>a) remove the coma after: form.keywords.value==&quot;&quot;)<br><br>b) if your searchtype is a select this will not work, this works only for text & hidden input fields. <br><br>I tested the follwowing function, it should work:<br><br>function OnSubmitKw(f) {<br> if (f.searchtype.text==&quot;Keyword&quot; && f.keywords.value==&quot;&quot;){<br> alert(&quot;You did not enter any Keywords! Please try again.&quot;);<br> }<br> else {<br> submit(f);<br> }<br>}<br><br><br>Hope this helps<br>Joern<br><br><br>
 
Dear DonP,<br><br>Your code:<br><br>function OnSubmitKw(form){<br>if (form.Keywords.value==&quot;&quot; && form.searchtype.value==&quot;Keyword&quot;){<br>&nbsp;&nbsp;&nbsp;alert(&quot;You did not enter any Keywords! Please try again.&quot;);}<br>}&nbsp;&nbsp;<br>**************<br><br>Assuming you have a form tag like this:<br><br>&lt;form onsubmit=&quot;OnSubmitKw(this)&quot; ...&gt;<br><br>Try changing to this:<br>&lt;form onsubmit=&quot;return OnSubmitKw(this)&quot; ...&gt;<br><br>function OnSubmitKw(form){<br>if (form.Keywords.value==&quot;&quot; && form.searchtype.value==&quot;Keyword&quot;){<br>&nbsp;&nbsp;&nbsp;alert(&quot;You did not enter any Keywords! Please try <br>&nbsp;&nbsp;return false;<br>again.&quot;);}<br>return true;<br>}&nbsp;&nbsp;<br><br>Hope this helps<br>-pete<br>
 
Thanks!&nbsp;&nbsp;It's better, but still not working. It was giving an error that I was missing a bracket but now it is not.&nbsp;&nbsp;I couldn't see that it had a comma somewhere! Thanks for correcting it.<br><br>&quot;searchtype&quot; is a hidden form tag and I have two others I want to &quot;Else&quot; into it once it is working (there are three different Keyword search forms for different purposes but with the searchtype having different hidden values). I am calling this JavaScript into each one as a separate file.<br><br>Now, when I enter a Keyword and press enter, it searches but when I press the Begin Search button, there is a JavaScript error.&nbsp;&nbsp;The same is true if I don't enter a Keyword.<br><br>This is what I have now, which is your code - Thanks ! - (I'm not sure what the word &quot;text&quot; is for though):<br><br>function OnSubmitKw(form) {<br>if (form.searchtype.text==&quot;Keyword&quot; && form.keywords.value==&quot;&quot;){<br>alert(&quot;You did not enter any Keywords! Please try again.&quot;);<br>}<br>else {<br>submit(form);<br>}<br>}<br><br>I think the problem now is the form itself, which is an HTML file. There are several forms on the one HTML document, each being opened and closed with its own pair for form tags, but this one has only this JavaScript for the Begin Search:<br>&lt;input type=&quot;button&quot; value=&quot;Begin Search&quot; onclick=&quot;OnSubmitKw(this.form)&quot;&gt;<br><br>DonP
 
first of all, what is the error?<br><br>it didn't really seem to clear to me if the search button and the text boxes were in the same form. since you are using 'this.form' as a parameter, make sure that the button is on the same form as the form you want submitted.&nbsp;&nbsp;Otherwise, just statically reference the form <p>ray<br><a href=mailto:rheindl@bju.edu>rheindl@bju.edu</a><br><a href= > </a><br>
 
Yes, the search button and the Keyword search boxes are on the same form.&nbsp;&nbsp;There are some other unrelated forms on the same HTML page though.&nbsp;&nbsp;I mentioned them only in case they might be a problem.<br><br>With palbano's suggested code (thanks!), I am now getting the proper alert if I press Enter or Submit Search without entering a keyword, and the form stays on the search page without loading the search script (that's exactly what I wanted), but entering a Keyword and submitting it also leaves me on the search page.&nbsp;&nbsp;It is not actually submitting and making a search.<br><br>Here is what I have now based on what palbano wrote, though I don't understand the last couple lines with the two return values:<br><br>function OnSubmitKw(form){<br>if (form.Keywords.value==&quot;&quot; && form.searchtype.value==&quot;Keyword&quot;){<br>alert(&quot;You did not enter any Keywords! Please try again.&quot;);}<br>return false;<br>return true;<br>}<br><br>The Begin Search button is still not a submit (should it be?):<br>&lt;input type=&quot;button&quot; value=&quot;Begin Search&quot; onclick=&quot;OnSubmitKw(this.form)&quot;&gt;<br><br>but I added the JavaScript back to the form tag, which I had removed based on an earlier suggestion:<br>&lt;form name=&quot;keyword&quot; method=&quot;post&quot; action=&quot;search.asp&quot; onsubmit=&quot;return OnSubmitKw(this)&quot;&gt;
 
The 'return false;' needs to be inside the brackets of the IF statement (after the alert message).&nbsp;&nbsp;right now, the form won't get submitted because once the 'return false' is reached, the function exits. Also, go ahead and make it a submit button, it will make things a little easier.&nbsp;&nbsp; <p>ray<br><a href=mailto:rheindl@bju.edu>rheindl@bju.edu</a><br><a href= > </a><br>
 
Hi Ray, I noticed that there was a typo in palbano's post and thought I had it corrected (I made an &quot;educated&quot; guess).&nbsp;&nbsp;But I was just beginning to realize the misplaced } when your message confirmed it. Thanks!<br><br>Now with this code, everything seems to be perfect except that when the Begin Search button is pressed without a keyword and the alert pops up, clicking OK it pops up again. Clicking it a second time leaves the search page ther just as it should.&nbsp;&nbsp;When submitting the search by pressing Enter, the alert pops up only once.&nbsp;&nbsp;Here is my code now:<br><br>&nbsp;function OnSubmitKw(form){<br>&nbsp;if (form.Keywords.value==&quot;&quot; && form.searchtype.value==&quot;Keyword&quot;){<br>&nbsp;alert(&quot;You did not enter any Keywords! Please try again.&quot;);<br>&nbsp;return false;<br>&nbsp;}<br>}<br><br>with these two related JavaScript form tags:<br><br>&lt;form name=&quot;keyword&quot; method=&quot;post&quot; action=&quot;search.asp&quot; onsubmit=&quot;return OnSubmitKw(this)&quot;&gt;<br><br>&lt;input type=&quot;submit&quot; value=&quot;Begin Search&quot; onclick=&quot;OnSubmitKw(this.form)&quot;&gt;<br><br>I think one of these is causing the extra alert. Except for that, it is working perfectly and my thanks to all who have helped!!<br><br>DonP
 
you do not need to have an onclick handler of a submit button call the form's submit function. just have the code say: &lt;input type=submit value='Begin Search'&gt; <p>ray<br><a href=mailto:rheindl@bju.edu>rheindl@bju.edu</a><br><a href= > </a><br>
 
Thanks everyone!&nbsp;&nbsp;I works perfectly now.&nbsp;&nbsp;I was about to try removing one or the other since I was pretty sure there needed to be only one in the form tags. Now that you've cleared it up, there is no guess work on my part!<br><br>I can't tell everyone how much I appreciate all their help.&nbsp;&nbsp;You are wonderful!<br><br>DonP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top