burntheblobs Posted November 29, 2008 Share Posted November 29, 2008 I have a set of 10 radio buttons, all with the same name, and I am trying to write something that checks to see if any button has been checked. The simple $value == "" doesn't seem to be working. I also would like to try to avoid checking to see if there isn't a match for every possible value. Quote Link to comment https://forums.phpfreaks.com/topic/134723-testing-whether-a-radio-button-was-checked/ Share on other sites More sharing options...
beansandsausages Posted November 29, 2008 Share Posted November 29, 2008 code? Quote Link to comment https://forums.phpfreaks.com/topic/134723-testing-whether-a-radio-button-was-checked/#findComment-701569 Share on other sites More sharing options...
burntheblobs Posted November 29, 2008 Author Share Posted November 29, 2008 <tr> <td> <input type='radio' name='experienceRating' value='0'> </td><td> <input type='radio' name='experienceRating' value='1'> </td><td> <input type='radio' name='experienceRating' value='2'> </td><td> <input type='radio' name='experienceRating' value='3'> </td><td> <input type='radio' name='experienceRating' value='4'> </td><td> <input type='radio' name='experienceRating' value='5'> </td><td> <input type='radio' name='experienceRating' value='6'> </td><td> <input type='radio' name='experienceRating' value='7'> </td><td> <input type='radio' name='experienceRating' value='8'> </td><td> <input type='radio' name='experienceRating' value='9'> </td><td> <input type='radio' name='experienceRating' value='10'> </td></tr> if ($field == "experienceRating") { if ($value == "") { echo "<font color='red'>You have not filled in a "; convertLabel($field); echo "!<br></font>"; $badform = "yes"; } Quote Link to comment https://forums.phpfreaks.com/topic/134723-testing-whether-a-radio-button-was-checked/#findComment-701575 Share on other sites More sharing options...
beansandsausages Posted November 29, 2008 Share Posted November 29, 2008 presuming ur using: <form action="myphp.php" method="POST" /> $experienceRating = $_POST['experienceRating']; if($empty($experienceRating)) { echo " empty "; } else { echo " not empty "; } some this like that ? Quote Link to comment https://forums.phpfreaks.com/topic/134723-testing-whether-a-radio-button-was-checked/#findComment-701591 Share on other sites More sharing options...
burntheblobs Posted November 30, 2008 Author Share Posted November 30, 2008 Thank you for the reply. I modified the code in a way that it SHOULD work, but it still doesn't work for some reason. Here is my code. if ($field == "Age" or $field == "firstRating" or $field == "secondRating" or $field == "thirdRating") { if (empty($_POST[$field])) { echo "<font color='red'>You have not filled in a "; convertLabel($field); echo "!<br></font>"; $badform = "yes"; } Quote Link to comment https://forums.phpfreaks.com/topic/134723-testing-whether-a-radio-button-was-checked/#findComment-702031 Share on other sites More sharing options...
.josh Posted November 30, 2008 Share Posted November 30, 2008 if your outer condition evaluates true then your inner condition will evaluate false, because in order for your outer condition to evaluate true, $field can't be empty. Quote Link to comment https://forums.phpfreaks.com/topic/134723-testing-whether-a-radio-button-was-checked/#findComment-702035 Share on other sites More sharing options...
.josh Posted November 30, 2008 Share Posted November 30, 2008 wait sorry that's wrong. Okay what I meant to say is, your inner condition will only execute if your outer one is true, and your outer one will only evaluate true if $field == one of those other things, like $_POST['Age'], not $_POST['experienceRating']. Where are you assigning something to $field? Quote Link to comment https://forums.phpfreaks.com/topic/134723-testing-whether-a-radio-button-was-checked/#findComment-702036 Share on other sites More sharing options...
burntheblobs Posted December 5, 2008 Author Share Posted December 5, 2008 All of this is inside a foreach loop. $field is the field name. $value is what is entered into the field. foreach ($_POST as $field => $value) Quote Link to comment https://forums.phpfreaks.com/topic/134723-testing-whether-a-radio-button-was-checked/#findComment-707140 Share on other sites More sharing options...
mrMarcus Posted December 5, 2008 Share Posted December 5, 2008 All of this is inside a foreach loop. $field is the field name. $value is what is entered into the field. foreach ($_POST as $field => $value) where is $field being generated? as far as i can see, $field has no value. and as far as your foreach loop, $field will be set as the key in the array. Quote Link to comment https://forums.phpfreaks.com/topic/134723-testing-whether-a-radio-button-was-checked/#findComment-707181 Share on other sites More sharing options...
burntheblobs Posted December 13, 2008 Author Share Posted December 13, 2008 I have a form in another spot in the program that generates the $field. I was under the impression that $_POST was an array that you could assign a key and a value to. It seems to be working for all my other processes. Sorry for not including code earlier, I realize this it's kind of hard to help me when I'm being stingy with code. Anyway, I was thinking that "experienceRating" would be the $field and $value would be whatever the value was. Quote Link to comment https://forums.phpfreaks.com/topic/134723-testing-whether-a-radio-button-was-checked/#findComment-714681 Share on other sites More sharing options...
burntheblobs Posted December 22, 2008 Author Share Posted December 22, 2008 Sorry to bump this, but this still isn't solved. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/134723-testing-whether-a-radio-button-was-checked/#findComment-721797 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.