Jump to content

Validating a simple radio button form **SOLVED**


Nomax5

Recommended Posts

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" value="Submit">
</FORM>
[/code]

if a visitor just clicks submit then off it goes with no data.

Is there a simple way of not going to nextpage.php unless they’ve made their selection?

Using isset or something?

If I use a phpself thing then I always have a problem getting to nextpage.php with headers already been sent etc.

Or do I have do the validation in nextpage.php then go back somehow?

I have been searching for a solution but they all seem like overkill.

Thanks
[code]<form action="nextpage.php" method="get">
<input type="radio" name="form_gen" value="male" checked="checked">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" checked="checked">ford
<input type="radio" name="form_car" value="jeep">jeep
<input type="submit" value="Submit">
</form>[/code]
That's with sucky formatting. here is an updated version.
[code]<form action="nextpage.php" method="get">
<input type="radio" name="form_gen" value="male" checked="checked" />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" checked="checked" />ford
<input type="radio" name="form_car" value="jeep" />jeep
<input type="submit" name="submit" id="submit" value="Submit">
</form>[/code]
You forgot to close those tags, and always name your submit buttons even if there just named submit,.
ahh thanks for the formating I just typed that in the post, it's formatted correctly in the code
honest ;)

so what you're saying is that if I default them then they can't clear them so the form will be valid.

The danger is that people may just take the defaults, although I could default to skoda :) 
sorry.
ok you don't want people to be able to submit information without making a choice.
Do this on hte page with the form roughly
[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_gen" value="none" checked="checked" />None
<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="radio" name="form_car" value="none" checked="checked" />None
<input type="submit" name="submit" id="submit" value="Submit">
</form>[/code]
None on your php page put this where you do validation if you append it to a screen, use exit, relocate, whatever you do to validate your forms
the setup is roughly like this
[code]
<?php
if ($_GET['formgen'] == "none" || $_GET['form_car'] == "none") {
// You must select values for the 2 radio buttons.  Whatever you do to validate here.
}
>>
[/code]
I just tried the following test:
[code]
<html>
<head>
<title>Test Form</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) echo '<pre>' . print_r($_POST,true) . '</pre>';
?>
<form method="post">
<span style="display:none"><input type="radio" name="test" value="default" checked></span><br>
<input type="radio" name=test value="one">&nbsp;One<br>
<input type="radio" name=test value="two">&nbsp;Two<br>
<input name="submit" type="submit" value="test">
</form>
</body>
</html>[/code]
You'll notice this line:
[code]<span style="display:none"><input type="radio" name="test" value="default" checked></span>[/code]
The [b]<span style="display:none">[/b] hides the default radio button from being seen, but the value still gets transmitted.

Ken

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.