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

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

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

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 :) 
Link to comment
Share on other sites

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

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