Page 1 of 1

Finding out which radio button has been checked

Posted: Sun Feb 23, 2003 2:55 pm
by nigma
Hey I have this form:

<p>How long have you been playing cs:<br>
<input type="radio" name="cstime" value="6 Months">6 Months<br>
<input type="radio" name="cstime" value="1 Year">1 Year<br>
<input type="radio" name="cstime" value="2 Years">2 Years<br>
<input type="radio" name="cstime" value="2 1/2 Years and up">2 1/2 Years and up<br><br>
</p>

I would like to know how I can get the value of the radio button that was pushed and store it into a variable in the php script that processes this form.

Thanks for all help and advice provided.

Posted: Sun Feb 23, 2003 3:07 pm
by twigletmac
If you put:

Code: Select all

echo '<pre>';
print_r($_POST);
echo '</pre>';
into the form processing script you will be able to see all of the information that has been posted. The information from the radio button will be available as $_POST['cstime'].

Mac

Posted: Sun Feb 23, 2003 3:11 pm
by nigma
So to store the info in a variable I would do:

$radioButtonPushed = $_POST[csTime];

Correct?

Posted: Sun Feb 23, 2003 3:16 pm
by twigletmac
Yup but put quotes around the array element

Code: Select all

$radioButtonPushed = $_POST['csTime'];
instead of

Code: Select all

$radioButtonPushed = $_POST[csTime];
See Array do's and don'ts on this page for why:
http://www.php.net/manual/en/language.types.array.php

Mac

Posted: Sun Feb 23, 2003 3:27 pm
by nigma
When I do that and then try to print the variable it just shows up blank.

Posted: Sun Feb 23, 2003 3:37 pm
by patrikG
Variables in PHP are case-sensitive.

Code: Select all

<?php$radioButtonPushed = $_POST['cstime'];?>
will give you the value of your radio-button after the form has been submitted.

Posted: Sun Feb 23, 2003 3:42 pm
by nigma
Excellent. I thought that I had made the name of the radio buttons csTime because I usually name things like that in PHP and in Perl.

Thanks a bunch for the help. I greatly appreciate it.