Jump to content

Recommended Posts

You must set the value of the radio button, and group radios together by name.

 

The html:

<html>
<label> <input type="radio" name="color" value="red" /> Red </label>
<label> <input type="radio" name="color" value="blue" /> Blue </label>
</html>

 

The code:

<?php
if ($_POST['color'] == 'red') {
    // the red button was checked
} else if ($_POST['color'] == 'blue') {
    // the blue button was checked
}
?>

I see what you're saying and that's how I thought of approaching the problem. However, I was learning/practicing javascript code and in javascript you're able to use a combination of "For" and "If" loops to determine which radio button is selected. For example, below is the code I put together in javascript

 

interval = document.getElementById('formName').fieldName.length;
for (i=0; i<interval; i++){
if(document.getElementById('formName').fieldName[i].checked){
var id = document.getElementById('formName').fieldName[i].value;
}
}

 

Basically, in the javascript code, an interval is automatically set based on the number of radio buttons and used in the "For" loop and then in the "If" loop the property of the radio button is evaluated to see if it is 'checked'. Note: in the javascript code, the radio button name is converted to an array by ".fieldName(i)" portion of the code. The javascript code seems so much more efficient than the PHP approach (i.e. What happens when there are 10 or 15 buttons?). So my follow-up question is: is there a way, similar to the javascript code, to determine which radio button is checked?

I did not have a particular situation in mind when I posted my original question. I do understand that with PHP, the way one deals with radio buttons is by using if/then statements to determine which button is selected.

 

I was just wondering, was it possible to determine which button was selected, as it was in the javascript code I posted.

 

A hypothetical situation where it may be beneficial to use the php equivalent (if there is one) of the javascript coding technique is when there are 8 options, for example, lets say that a user must select a direction. And the options are: North, North-East, East, South-East, South, South-West, West, North-West. Using the conventional way in PHP, one would use if/else if statements that could turn out to be lengthy.

Okay so if you are wanting php to perform a different action based on what was selected, then yes, you would use multiple if statements, or if/elseif/else statements, or a switch statement, or nested conditions, or whatever.  Really depends on the situation. 

 

Variables posted from a form get stored in $_GET['varnamehere'] or $_POST['varnamehere'] (depending on the form method; can you guess which goes where?).  From there, you would simply use conditions...just like flyhoney posted in his/her first post. 

The difference between javascript and PHP in this case, is that in javascript the radio buttons are all contained in the DOM so therefore they are accessbile.  However, in PHP, the only information you have access to is the information that was submitted via the form.  When you submit a form with a radio button, only the value of the selected button is set, the others are not present.

Thank you Crayon Violent and flyhoney for your responses. Flyhoney, your latest response got to the heart of the matter; in your post you addressed the fundamental difference between the way radio buttons are treated in javascript and php. By doing so, you clearly and concisely addressed and resolved my question. Thanks.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.