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?


Link to comment
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]

Link to comment
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]
Link to comment
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.
Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.