Jump to content

Checking if radio button is used


bukwus

Recommended Posts

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

 

Link to comment
Share on other sites

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??  :shrug:  Just a thought.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

@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)

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

<?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>";}	
?>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.