bukwus Posted June 23, 2010 Share Posted June 23, 2010 Hi I'm using PHP to check if a form was filled out correctly, or at all. I have questions and three answers in the form of radio buttons. I'm using "if empty" to check if a question wasn't answered but it's not doing the trick: for ($i = 1; $i <= $numQuestions2; $i++) { if (empty($_POST['radio'.$i])) { $problem = TRUE; $dataSpan2[$i] = '<span style="color:red; font-weight:bold;">'.$questions2[$i].'</span>'; } else { $dataSpan2[$i] = $questions2[$i]; } } How should I be doing this? Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/205667-checking-if-radio-button-is-used/ Share on other sites More sharing options...
TOA Posted June 23, 2010 Share Posted June 23, 2010 I'm using "if empty" to check if a question wasn't answered but it's not doing the trick: If a radio button is not selected, it POSTs no data. It won't even know it existed in the form. One solution (though very crude) is to make the radios select one by default, but in your scenario that wouldn't work. Other than that, I'm unaware of a way to test if a radio is selected or not. You could maybe see how many POSTs there are and compare to total number of questions to see if any went unanswered?? Just a thought. Quote Link to comment https://forums.phpfreaks.com/topic/205667-checking-if-radio-button-is-used/#findComment-1076240 Share on other sites More sharing options...
bukwus Posted June 23, 2010 Author Share Posted June 23, 2010 Hmmm Thanks Devils. I'll see what I can do. Quote Link to comment https://forums.phpfreaks.com/topic/205667-checking-if-radio-button-is-used/#findComment-1076247 Share on other sites More sharing options...
gwolgamott Posted June 23, 2010 Share Posted June 23, 2010 If you are using radio buttons instead of checkboxes that gives the notion of that at least one of them must be selected in a group... Thus you just go by the value of the radio button group. Since they are designed to have a group name and the value is the selected item of that group. But if you don't want it have a default value and want to check if it has a no value then just do that... <html> <form action="simple_test.php" method="post"> <input type="radio" name="sex" value="male" /> Male<br /> <input type="radio" name="sex" value="female" /> Female<br> <input type="submit" name="submit" value="click me" /> </form> <?php // No checked at either of the inputs... so the value is nothing... // // This represents a default value <input type="radio" name="sex" value="female" checked /> // if(!isset($_POST["sex"])){echo "No radio checked";} else { echo $_POST["sex"];} ?> </html> Quote Link to comment https://forums.phpfreaks.com/topic/205667-checking-if-radio-button-is-used/#findComment-1076249 Share on other sites More sharing options...
TOA Posted June 23, 2010 Share Posted June 23, 2010 @gwolgamutt I tried your code, and that won't work because if there is nothing selected there will be nothing to compare to. Try this... if ($_SERVER['REQUEST_METHOD'] == "GET") { echo '<form method="post"><input type="radio" name="sex" value="male" /> Male<br /><input type="radio" name="sex" value="female" /> Female<br /><input type="submit" value="Submit" /></form>'; } else { foreach ($_POST as $key => $value) { echo $key.' : '.$value; } } If you don't select anything, and just hit enter, there will be no POST data, which is the problem with radio's. If we could test for a blank in the manner you suggested, it would POST back "sex : " if there was a blank, but instead, the name doesn't even get sent to compare a blank value against (Unless there's something I'm missing in my knowledge base and if so please point it out) Quote Link to comment https://forums.phpfreaks.com/topic/205667-checking-if-radio-button-is-used/#findComment-1076257 Share on other sites More sharing options...
gwolgamott Posted June 23, 2010 Share Posted June 23, 2010 @gwolgamutt I tried your code, and that won't work because if there is nothing selected there will be nothing to compare to. Try this... if ($_SERVER['REQUEST_METHOD'] == "GET") { echo '<form method="post"><input type="radio" name="sex" value="male" /> Male<br /><input type="radio" name="sex" value="female" /> Female<br /><input type="submit" value="Submit" /></form>'; } else { foreach ($_POST as $key => $value) { echo $key.' : '.$value; } } If you don't select anything, and just hit enter, there will be no POST data, which is the problem with radio's. If we could test for a blank in the manner you suggested, it would POST back "sex : " if there was a blank, but instead, the name doesn't even get sent to compare a blank value against (Unless there's something I'm missing in my knowledge base and if so please point it out) It's the !isset. if(!isset()){} So if it is Not set then run that if statement. Isset() checks if it is set and not null... so when it does not exist thus it not set and since it doesn't exist it's can't be null. Quote Link to comment https://forums.phpfreaks.com/topic/205667-checking-if-radio-button-is-used/#findComment-1076261 Share on other sites More sharing options...
gwolgamott Posted June 23, 2010 Share Posted June 23, 2010 If you don't select anything, and just hit enter, there will be no POST data, which is the problem with radio's. Not a problem they were designed in mind that one of the groups of radio buttons would be selected. Since radios is a select one of these not an optional select. Not that they weren't built to allow you to do otherwise... but isset() is designed to handle just that to check if something is set.... and null is considered set. So if it does not exist then it is not set. That is why it is common to see !isset() and isset() on top of form page scripts to determine if intial run or a call from itself. Quote Link to comment https://forums.phpfreaks.com/topic/205667-checking-if-radio-button-is-used/#findComment-1076267 Share on other sites More sharing options...
TOA Posted June 24, 2010 Share Posted June 24, 2010 I'm familiar with it thanks. But how can you check if something isset if it doesn't even exist? If you try to check a variable that doesn't exist, you just get nothing. If you have a working example I'd love to see it, I'll whip something up to show my point in a few, got a little bit of actual work to do first :'( (Work...who needs it) Quote Link to comment https://forums.phpfreaks.com/topic/205667-checking-if-radio-button-is-used/#findComment-1076597 Share on other sites More sharing options...
gwolgamott Posted June 24, 2010 Share Posted June 24, 2010 <?php if(!isset($_POST["no_exist"])){echo "I don't Exist <br>";} else { echo "not in the if statement 1 <br>";} if($_POST["no_exist"] === NULL ){echo "I still do not exist <br>";} else { echo "not in the if statement 2 <br>";} if(empty($_POST["no_exist"])){echo "And yet I still do not exist. <br>";} else { echo "not in the if statement 3 <br>";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/205667-checking-if-radio-button-is-used/#findComment-1076610 Share on other sites More sharing options...
gwolgamott Posted June 24, 2010 Share Posted June 24, 2010 Realized I misspoke... so to correct myself. isset() checks if a variable is set and not NULL. Previously I stated that wrong saying it was true only if it was set and did not have a null value... not sure where my head was on that one. Quote Link to comment https://forums.phpfreaks.com/topic/205667-checking-if-radio-button-is-used/#findComment-1076683 Share on other sites More sharing options...
TOA Posted June 24, 2010 Share Posted June 24, 2010 Realized I misspoke... so to correct myself. isset() checks if a variable is set and not NULL. Previously I stated that wrong saying it was true only if it was set and did not have a null value... not sure where my head was on that one. I knew what you meant Quote Link to comment https://forums.phpfreaks.com/topic/205667-checking-if-radio-button-is-used/#findComment-1076713 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.