trampolinejoe Posted April 26, 2009 Share Posted April 26, 2009 Hello People, I am starting with some basic form validation with js. How do I stop this posting to the next page if the error occurs? No matter what I do even if the error occurs it seems to post to the next page. My JS Function: function empty(){ if ( document.form1.products.value == "empty" ) { alert ( "You have not selected a product, please select a product." ); valid = false; } } Drop Down Menu: <select name="productArray" id="productArray" onChange="change()"> <option value="empty">--Please Select--</option> <option value="8fttrampoline2009">8ft Trampoline <?php echo'$'."$eightfttrampoline".'.00'; ?></option> <option value="10fttrampoline2009">10ft Trampoline <?php echo'$'."$tenfttrampoline".'.00'; ?></option> </select> Submit Button: <input name="next" type="submit" onClick="return empty()" value="Next >>"> Quote Link to comment https://forums.phpfreaks.com/topic/155671-solved-stop-javascript-form-submit/ Share on other sites More sharing options...
darkfreaks Posted April 26, 2009 Share Posted April 26, 2009 Works Fine For me: <script type = "text/javascript" language = "Javascript"> function empty(){ if ( document.form1.productArray.value == "empty" ) { alert("You have not selected a product, please select a product."); valid = false; } } </script> <form name="form1"> <select name="productArray" id="productArray"> <option value="empty">--Please Select--</option> <option value="8fttrampoline2009">8ft Trampoline </option> <option value="10fttrampoline2009">10ft Trampoline </option> </select> <input name="next" type="submit" onClick="return empty()" value="Next >>"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/155671-solved-stop-javascript-form-submit/#findComment-819373 Share on other sites More sharing options...
trampolinejoe Posted April 26, 2009 Author Share Posted April 26, 2009 Try changing your form tag to action='example.php' and see if it stills works fine. I think it maybe no posting for you because it dosn't know what to do. Other then that all I can think of it the fact I have a separate file for my js. Quote Link to comment https://forums.phpfreaks.com/topic/155671-solved-stop-javascript-form-submit/#findComment-819383 Share on other sites More sharing options...
darkfreaks Posted April 26, 2009 Share Posted April 26, 2009 can you poste the whole code then plz Quote Link to comment https://forums.phpfreaks.com/topic/155671-solved-stop-javascript-form-submit/#findComment-819385 Share on other sites More sharing options...
trampolinejoe Posted April 26, 2009 Author Share Posted April 26, 2009 Hello, Thanks for the fast response! I should have posted it as a single thing from the 1st post. I have included the html I used got my form, other then that it's pretty much the same. Cheers. <script type = "text/javascript" language = "Javascript"> function empty(){ if ( document.form1.productArray.value == "empty" ) { alert("You have not selected a product, please select a product."); valid = false; } } </script> <form name="form1" method="post" action="onePost.php" > <select name="productArray" id="productArray"> <option value="empty">--Please Select--</option> <option value="8fttrampoline2009">8ft Trampoline </option> <option value="10fttrampoline2009">10ft Trampoline </option> </select> <input name="next" type="submit" onClick="return empty()" value="Next >>"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/155671-solved-stop-javascript-form-submit/#findComment-819388 Share on other sites More sharing options...
darkfreaks Posted April 26, 2009 Share Posted April 26, 2009 without onpost.php its going to be useless Quote Link to comment https://forums.phpfreaks.com/topic/155671-solved-stop-javascript-form-submit/#findComment-819389 Share on other sites More sharing options...
trampolinejoe Posted April 26, 2009 Author Share Posted April 26, 2009 Well, onePost.php is a blank page atm, I havn't started coding it because I am not up to that yet. I was under the impression that the form validation could be done on the same page with js and that the contents of the page which posts is not important at this stage because we shouldn't even be posting to that page under the condition that the 'empty' option is selected we should get the alert and then stay on the same page. Please correct me if I am wrong. Cheers, Joe. Quote Link to comment https://forums.phpfreaks.com/topic/155671-solved-stop-javascript-form-submit/#findComment-819392 Share on other sites More sharing options...
darkfreaks Posted April 26, 2009 Share Posted April 26, 2009 could do action=<?=$_PHP_SELF;?> Quote Link to comment https://forums.phpfreaks.com/topic/155671-solved-stop-javascript-form-submit/#findComment-819393 Share on other sites More sharing options...
trampolinejoe Posted April 26, 2009 Author Share Posted April 26, 2009 I am sure you could post to the same page, or you could even point to a page that you do not have either doesn't make a difference to the fact that it is still submitting the page. However, I have noticed why it is doing this, I forgot to make the function 'return false' and that was the cause of the issue. I thank you for your time anyways. Cheers, Joe. Quote Link to comment https://forums.phpfreaks.com/topic/155671-solved-stop-javascript-form-submit/#findComment-819398 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.