How to validate radio buttons

Hello,

I can’t validate the radio button, each time i fill the captcha and hit the submit button the message is send. I try to validate the radio button so each time i hit the submit button to get the error like below.

$submission_status = '<div class="info" align="left">Please select gender </div>';

Could somebody explain me how to fix that ?

<?php

session_start();
ob_start();

if(isset($_POST["submitted"]) && $_POST["submitted"] == 1)
{
	
	$to_email          = "example@example.com"; 

	$security_code     = trim(strip_tags($_POST['captcha_code']));
	$gender = "";
	
   if (isset($_POST['group1'])) {
            $gender = $_POST['group1'];
        } else {
            $submission_status = '<div class="info" align="left">Please select gender </div>';
        }
	if(!isset($_SESSION['captcha_code']))
	{
		$submission_status = '<div class="info" align="left">Sorry, there is no session</div>';
	}
	else
	{
		if(empty($_SESSION['captcha_code']) || strcasecmp($_SESSION['captcha_code'], $_POST['captcha_code']) != 0)
		{
			
			$submission_status = '<div class="info" align="left">Sorry, the security code you provided was incorrect, try again.</div>';
		}
		else
		{
			$mailer_delivers_greatly = @mail($to_email, $gender, $headers);
					
			if ($mailer_delivers_greatly) 
			 {
				//Displays the success message when email message is sent
				  $submission_status = "<div class='alert alert-success'>Success</div>";
			 } 
			 else 
			 {
				 //Displays an error message when email sending fails
				  $submission_status = "<div align='left' class='info'>Error</div>";
			 }
		}
	}
}

?>

And the radio buttons i want to validate

<form method="post" action="test_mail.php" enctype="multipart/form-data">

<label>
    <input type="radio" name="group1" value="Male" />Male
</label>

<label>
   <input type="radio" name="group1" value="Female" />Female
</label>

   
<input type="hidden" id="submitted" name="submitted" value="1">
<input type="submit" class=" btn btn-sm btn-success"  value="Submit">
</form>

Thanks in advance !

You could set a variable “flag” to true, then as you check each post field, if “bad”, set that “flag” to false. Then wrap the email section in the “flag” being true condition.
Example:

<?php
session_start();
ob_start();


if(isset($_POST['submitted']) && $_POST['submitted'] == 1)
{

    $to_email          = "example@example.com"; 
    $headers = "From: \"Site Name\" <info@domain.com>\r\n";
    $security_code     = trim(strip_tags($_POST['captcha_code']));
    $gender = "";
    //Set flag to true
    $good =  true;
    
    if (isset($_POST['group1'])) {
        $gender = $_POST['group1'];
    } else {
        $submission_status = '<div class="info" align="left">Please select gender </div>';
        $good = false;
    }
    if(!isset($_SESSION['captcha_code']))
    {
        $submission_status = '<div class="info" align="left">Sorry, there is no session</div>';
        $good = false;
    }
    else
    {
        if(empty($_SESSION['captcha_code']) || strcasecmp($_SESSION['captcha_code'], $_POST['captcha_code']) != 0)
        {        
            $submission_status = '<div class="info" align="left">Sorry, the security code you provided was incorrect, try again.</div>';
            $good = false;
        }
    }
    
    if($good){
        $mailer_delivers_greatly = mail($to_email, $gender, $headers);
        
        if ($mailer_delivers_greatly) 
        {
            //Displays the success message when email message is sent
            $submission_status = "<div class='alert alert-success'>Success</div>";
        } 
        else 
        {
            //Displays an error message when email sending fails
            $submission_status = "<div align='left' class='info'>Error</div>";
        }
    }
}
?>
1 Like

@Tasos,

You can try to check if it is empty

         $group1=(empty($_POST['group1'])) ? "Please select gender": $_POST['group1'];

Hope this helps.

For security I would also check that the value is either “Male” or “Female” before assigning it to $group1

It’s better to check with isset() for radio buttons as they may or may not BE SET.

[quote=“Mittineague, post:4, topic:110900”]
For security I would also check that the value is either “Male” or “Female” before assigning it to $group1
[/quote]I totally agree that wherever possible compare post to the expected value especially if inserting to a DB. This is for emailing (at least thus far) but learning to validate user input is a very important aspect of coding.

This is a very simple example, but if you were to make an array of expected values (at the top of the page)…

$genders = array("Male","Female");

You could then use this array to build your radio button selections…

<?php
foreach($genders as $gender):
?>
<label>
    <input type="radio" name="group1" value="<?php echo $gender;?>" /><?php echo $gender;?>
</label>
<?php
endforeach;
?>

Then on processing, you can then check if posted value is in_array so only expected values will pass.

if (isset($_POST['group1']) && in_array($_POST['group1'],$genders)) {
    $gender = trim($_POST['group1']);
}else{
    $submission_status = '<div class="info" align="left">Please select gender </div>';
    $good = false;
}

Anyway, “code” for thought.

Thank allot Drummin, it works great !

But know i got the problem like the value, i just figure it out you can manipulate the value and send what ever you want !

EDIT: I tried to do what Mittineague said but i can’t make it work

As far as checking the values, add the $genders array like I mentioned and do the in_array check.

I get this error,

Warning: Invalid argument supplied for foreach() in … on line 284
Wich is

<?php foreach($genders as $gender):?>

Thanks allot for your help.

What I meant was to add the genders array at the top of your page.

$genders = array("Male","Female");

Then update the group1 processing condition like so

if (isset($_POST['group1']) && in_array($_POST['group1'],$genders)) {
1 Like

Thanks it works.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.