Finding out which radio button has been checked

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Finding out which radio button has been checked

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

So to store the info in a variable I would do:

$radioButtonPushed = $_POST[csTime];

Correct?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

When I do that and then try to print the variable it just shows up blank.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

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