Selecting values from radio field

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
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Selecting values from radio field

Post by mcccy005 »

I'm trying to determine the value submitted in a radio field but having a few problems for some reason.

I'm using a radio field with 2 options - Yes and No. (Yep, I know I really should use a checkbox for this, but a lot of the people who will use the website may be extremely computer illiterate and so black and white 'Yes' or 'No' is easier for them to understand than a checkbox!!)

Anyways....
If I get the value of the radio field using $_REQUEST[$field_name], the value is ALWAYS 'Yes' whether I select 'Yes' or 'No' in the radio field.
Even in the html code, the value of each radio option is correct!

Perhaps there's some alternate way to get the values??
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

Can we see the HTML!

pif!
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Post by phpCCore Brad »

Code: Select all

<input type="radio" name="yesorno" value="Yes">Yes 
<input type="radio" name="yesorno" value="No">No

//Should Yield

$_REQUEST["yesorno"] = "Yes"; //If the yes radio is clicked
$_REQUEST["yesorno"] = "No";  //If the no radio is clicked
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post by technofreak »

Why don't you just use $_POST than using $_REQUEST ?

The code will be then...

Code: Select all

if ($_POST['yesorno'] == 'yes') {

<code>

} elseif ($_POST['yesorno'] == 'no') {

<code>

}
In your code, you are trying to assign "yes" to the $_REQUEST variable in the first line and again trying to asign 'no" to the same variable. Think if you meaning to check the value there, please use "==" instead of "=". I have made the same mistakes and wondered why the if condition is not working :)
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Post by phpCCore Brad »

I was just posting an example for him. That wasn't meant to be PHP it was meant to tell him would the $_REQUEST would be set to. And the reason I used $_REQUEST instead of $_POST is I have no idea if his form is get or post.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

Isn't guessing fun, :wink:

pif!
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Was half way through writing back ("knowing" I woulnd't/couldnt have done anything as silly as missing out on double equals and um....well yeah.
Thanks - I have a bad habbit of doing stupid stupid little things in programs.

PS. The reason I always use $_REQUEST is because a lot of what I'm writing is objects which I will implement later for future projects and so it will allow me to use $_GET or $_POST.
Suppose I should research security issues relating to $_REQUEST though!
Post Reply