Jump to content

Archived

This topic is now archived and is closed to further replies.

Nomax5

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

Recommended Posts

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?


Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
That works a dream in my IE

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.