View Full Version : Radio box selection hell :(

11-08-2007, 10:29 AM
i have no idea whats going on with this stoooopid thing! i am soo frustrated!
i have gender radio buttons which look like this

<label for="gender">Gender:</label>
<input type="radio" name="gender" value="male" <?=($gender == 'male' ? 'checked' : ''); ?> >male
<input type="radio" name="gender" value="female" <?=($gender == 'female' ? 'checked' : ''); ?> >female
<font size="2" color="red"><?php print "$gendermessage" ?></font><p>

i then have a little validation code above the html which goes like this

if (empty($_POST['gender']) || ($_POST['gender'] != "male" && $_POST['gender'] != "female"))
$gendermessage = "Please enter a gender ";
$errors ++;

the problem is when i click female and print $gender after submit it prints female even if i pressed male.
when i click submit and the page is caught by another field the radio button does not stay empty or at the box which was previously chosen it snaps to female!

consequently i cannot distinguish between male and female which means none of my application works :(
i'm totally baffled- i know i've been asking alot but does anyone know why this is happening?
in much desperation

11-08-2007, 10:40 AM
if ($_POST['gender'] != "male" && $_POST['gender'] != "female"))
$gendermessage = "Please enter a gender ";
$errors ++;

try this?

11-08-2007, 10:49 AM
Thanks psykx:)
unfortunately thats not changed anything, it still snaps to female after submitted.

11-08-2007, 11:48 AM
is there any code to deal with the input? e.g

if ($_POST['gender'] = "male" )
male do
} else {
female do

11-08-2007, 04:53 PM
Your variable $gender is set ... where? Do you have register_globals turned on?

Other things to nag you about... your <label> tag is inproperly written. You should have two <label> tags enclosing the text "male" and the text "female", and should be for="male" and for="female", with an id="male" and an id="female" inside the input elements.

Oh and the correct syntax for checking a radio button is checked="checked" rather than the deprecated "checked".

11-08-2007, 05:04 PM
Do you have register_globals turned on?

If you do, turn it off! ;)

11-08-2007, 11:15 PM
Thank you, i found why it was snapping to female- i'm rather embarrassed to say but i had declared $gender = "female"; !!! it was lost in a stack of variables so i missed it- still no excuse for such a sloppy mistake which caused so much sleep loss!
Thanks for the advice about register_globals i will look into them and bear it in mind next time i get into problems which i bet will be soon;)
my project is finished now and it works wonderfully i am very pleased- on with project 2:)
Thanks again for helping.