Jump to content


Photo

Validating a simple radio button form **SOLVED**


  • Please log in to reply
12 replies to this topic

#1 Nomax5

Nomax5
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationYork England

Posted 28 July 2006 - 06:44 PM

Suppose I have this form containing only radio buttons
<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>

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


#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 28 July 2006 - 06:49 PM

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 06:51 PM

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

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#4 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 06:52 PM

That's with sucky formatting. here is an updated version.
<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>
You forgot to close those tags, and always name your submit buttons even if there just named submit,.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#5 Nomax5

Nomax5
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationYork England

Posted 28 July 2006 - 07:05 PM

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 :) 


#6 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 28 July 2006 - 07:26 PM

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.

#7 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 07:28 PM

PERFECT, hostfreak had a perfect idea, that would work like a miracle, I have to keep that in mind for later.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#8 Nomax5

Nomax5
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationYork England

Posted 28 July 2006 - 08:14 PM

I dont' understand hostfreak how do I add values to defaults?


#9 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 08:21 PM

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

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#10 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 08:28 PM

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
<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>
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
<?php
if ($_GET['formgen'] == "none" || $_GET['form_car'] == "none") {
// You must select values for the 2 radio buttons.  Whatever you do to validate here.
}
>>

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#11 Nomax5

Nomax5
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationYork England

Posted 28 July 2006 - 09:06 PM

Ohh I see radio buttons for none, I get it thanks matey really good answer




#12 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 28 July 2006 - 09:21 PM

I just tried the following test:
<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>
You'll notice this line:
<span style="display:none"><input type="radio" name="test" value="default" checked></span>
The <span style="display:none"> hides the default radio button from being seen, but the value still gets transmitted.

Ken


#13 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 09:22 PM

That is an even better idea, I will have to keep that in mind.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users