Jump to content

Archived

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

Nomax5

Validating a simple radio button form **SOLVED**

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

Share this post


Link to post
Share on other sites
Using PHP yes you would have to submit to a page and then validate.. If you look into Javascript or post a request in javascript forum then i'm sure that they can help you there :)

Regards
Liam

Share this post


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

Share this post


Link to post
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,.

Share this post


Link to post
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 :) 

Share this post


Link to post
Share on other sites
Add values to your defualts. Then check to see if the values are the ones of the defualt, if so don't allow it to be submitted.

Share this post


Link to post
Share on other sites
PERFECT, hostfreak had a perfect idea, that would work like a miracle, I have to keep that in mind for later.

Share this post


Link to post
Share on other sites
I dont' understand hostfreak how do I add values to defaults?

Share this post


Link to post
Share on other sites
value="none"
then when it get's to the next page use something to validate that
if $_POST['whatever'] == "none']
there was an error
sorry don't have time to format

Share this post


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

Share this post


Link to post
Share on other sites
Ohh I see radio buttons for none, I get it thanks matey really good answer


Share this post


Link to post
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

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.