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