Nomax5 Posted July 28, 2006 Share Posted July 28, 2006 I asked this question on the PHP help and one suggested I came here. Suppose I have this form containing only radio buttons[code]<form action="nextpage.php" method="get"><input type="radio" name="form_gen" value="male" />Male<input type="radio" name="form_gen" value="female" />Female<input type="radio" name="form_car" value="bmw" />BMW<input type="radio" name="form_car" value="ford" />ford<input type="radio" name="form_car" value="jeep" />jeep<input type="submit" name="submit" id="submit" value="Submit"> </form>[/code]if a visitor just clicks submit then off it goes with no data.Is there a simple way of checking the visitor made a selection before it goes to nextpage.php? using isset or something? Quote Link to comment https://forums.phpfreaks.com/topic/15920-pre-validating-a-form-prior-to-post-solved/ Share on other sites More sharing options...
sanfly Posted July 29, 2006 Share Posted July 29, 2006 Using this tutorial [url=http://webdevelopersjournal.com/articles/jscript_forms1.html]here[/url], I came up with the code below. It uses onSubmit to check the fields are not empty, and fails the submit action if they are.[code]<html> <head> <script> function validate(){ // Check Gender if(!(myForm.form_gen[0].checked || myForm.form_gen[1].checked)){ alert('You must select your gender'); event.returnValue = false; } //Check Car if(!(myForm.form_car[0].checked || myForm.form_car[1].checked || myForm.form_car[2].checked)){ alert('You must select your car type'); event.returnValue = false; } } </script> </head> <body bgcolor="#ffffff"> <form name="myForm" action="nextpage.php" method="get" onsubmit="validate();"><input type="radio" name="form_gen" value="male" />Male<input type="radio" name="form_gen" value="female" />Female<br><br><input type="radio" name="form_car" value="bmw" />BMW<input type="radio" name="form_car" value="ford" />ford<input type="radio" name="form_car" value="jeep" />jeep<input type="submit" name="submit" id="submit" value="Submit"> </form> </body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15920-pre-validating-a-form-prior-to-post-solved/#findComment-65649 Share on other sites More sharing options...
Nomax5 Posted July 30, 2006 Author Share Posted July 30, 2006 That works a dream in my IE but firefox seems to ignore it....any ideas ? Quote Link to comment https://forums.phpfreaks.com/topic/15920-pre-validating-a-form-prior-to-post-solved/#findComment-66008 Share on other sites More sharing options...
sanfly Posted July 30, 2006 Share Posted July 30, 2006 Give this a go[code]<html> <head> <script> function validate(event){ if(navigator.userAgent.indexOf("Firefox") != -1){ event.preventDefault(); } // Check Gender if(!(myForm.form_gen[0].checked || myForm.form_gen[1].checked)){ alert('You must select your gender'); if(navigator.userAgent.indexOf("Firefox") != -1){ event.preventDefault(); } else{ event.returnValue = false; } } //Check Car if(!(myForm.form_car[0].checked || myForm.form_car[1].checked || myForm.form_car[2].checked)){ alert('You must select your car type'); if(navigator.userAgent.indexOf("Firefox") != -1){ event.preventDefault(); } else{ event.returnValue = false; } } } </script> </head> <body bgcolor="#ffffff"> <form name="myForm" action="nextpage.php" method="get" onsubmit="validate(event);"><input type="radio" name="form_gen" value="male" />Male<input type="radio" name="form_gen" value="female" />Female<br><br><input type="radio" name="form_car" value="bmw" />BMW<input type="radio" name="form_car" value="ford" />ford<input type="radio" name="form_car" value="jeep" />jeep<input type="submit" name="submit" id="submit" value="Submit"> </form> </body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15920-pre-validating-a-form-prior-to-post-solved/#findComment-66168 Share on other sites More sharing options...
Nomax5 Posted August 5, 2006 Author Share Posted August 5, 2006 Yeah That works great thanks.I actually used select instead of radio buttons because selects are easier to click. but works fine with either.no alert in firefox but they'll figure it out.Thanks once again Sir. Quote Link to comment https://forums.phpfreaks.com/topic/15920-pre-validating-a-form-prior-to-post-solved/#findComment-69763 Share on other sites More sharing options...
sanfly Posted August 5, 2006 Share Posted August 5, 2006 Your welcome.And not sir, miss :P Quote Link to comment https://forums.phpfreaks.com/topic/15920-pre-validating-a-form-prior-to-post-solved/#findComment-69969 Share on other sites More sharing options...
Nomax5 Posted August 6, 2006 Author Share Posted August 6, 2006 I do beg your pardon, I just keyed the thankyou in without thinking - sorry.I am guilty of assuming you were male, and that's wrong, so I am sorry.I can't say much else because I have a size nine boot in my mouth. Quote Link to comment https://forums.phpfreaks.com/topic/15920-pre-validating-a-form-prior-to-post-solved/#findComment-70206 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.