Dan06 Posted November 5, 2008 Share Posted November 5, 2008 Suppose there are X (at least more than 1) number of radio buttons in a form. What would be the best way to determine which radio button is 'checked?' Quote Link to comment https://forums.phpfreaks.com/topic/131512-determining-which-radio-button-is-checked/ Share on other sites More sharing options...
flyhoney Posted November 5, 2008 Share Posted November 5, 2008 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/131512-determining-which-radio-button-is-checked/#findComment-683010 Share on other sites More sharing options...
Dan06 Posted November 5, 2008 Author Share Posted November 5, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/131512-determining-which-radio-button-is-checked/#findComment-683021 Share on other sites More sharing options...
flyhoney Posted November 5, 2008 Share Posted November 5, 2008 I think perhaps radio buttons are not the best choice for whatever you are trying to do. What exactly are you trying to accomplish? Maybe there is a better approach... Quote Link to comment https://forums.phpfreaks.com/topic/131512-determining-which-radio-button-is-checked/#findComment-683023 Share on other sites More sharing options...
.josh Posted November 5, 2008 Share Posted November 5, 2008 Unless there is some reason you are wanting to keep the values of the other radio buttons, there's no reason why you shouldn't be doing it the way flyhoney originally suggested. Quote Link to comment https://forums.phpfreaks.com/topic/131512-determining-which-radio-button-is-checked/#findComment-683029 Share on other sites More sharing options...
flyhoney Posted November 5, 2008 Share Posted November 5, 2008 I think maybe you need to be using checkboxes. Quote Link to comment https://forums.phpfreaks.com/topic/131512-determining-which-radio-button-is-checked/#findComment-683040 Share on other sites More sharing options...
Dan06 Posted November 5, 2008 Author Share Posted November 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/131512-determining-which-radio-button-is-checked/#findComment-683287 Share on other sites More sharing options...
.josh Posted November 6, 2008 Share Posted November 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/131512-determining-which-radio-button-is-checked/#findComment-683323 Share on other sites More sharing options...
flyhoney Posted November 6, 2008 Share Posted November 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/131512-determining-which-radio-button-is-checked/#findComment-683728 Share on other sites More sharing options...
Dan06 Posted November 8, 2008 Author Share Posted November 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/131512-determining-which-radio-button-is-checked/#findComment-684999 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.