Jump to content

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


Nomax5

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?


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]

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]

Archived

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

×
×
  • Create New...

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.