Jump to content


Photo

Pre validating a form prior to post *** SOLVED ***


  • Please log in to reply
6 replies to this topic

#1 Nomax5

Nomax5
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationYork England

Posted 28 July 2006 - 07:12 PM

I asked this question on the PHP help and one suggested I came here.

Suppose I have this form containing only radio buttons
<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>
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?




#2 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 29 July 2006 - 11:51 PM

Using this tutorial here, 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.

<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>


If you're not part of the solution, you're part of the precipitate

#3 Nomax5

Nomax5
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationYork England

Posted 30 July 2006 - 05:23 PM

That works a dream in my IE

but firefox seems to ignore it....any ideas ?


#4 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 30 July 2006 - 11:53 PM

Give this a go

<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>

If you're not part of the solution, you're part of the precipitate

#5 Nomax5

Nomax5
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationYork England

Posted 05 August 2006 - 12:43 PM

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.


#6 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 05 August 2006 - 10:20 PM

Your welcome.

And not sir, miss  :P
If you're not part of the solution, you're part of the precipitate

#7 Nomax5

Nomax5
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationYork England

Posted 06 August 2006 - 02:07 PM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users