I have some code that displays input information from a checkbox in my email once submitted. This works absolutely fine but when a user does not make a selection from the checkboxes I want it to display ‘No options checked’ in my email message. How can I do this? Below is the relevant code – I need to achieve this for both of these checkboxes. I am assuming I need some sort of ELSE statement.
Code:
if(!empty($_POST['features'])) {
foreach($_POST['features'] as $value) {
$check_msg1 .= "Features checked: ".$value."\n";
}}
if(!empty($_POST['fright'])) {
foreach($_POST['fright'] as $value) {
$check_msg2 .= "Fright checked: ".$value."\n";
}}
try:
If no options are checked, then POST will not return any value for a checkbox. Therefore, you need to check to see whether the value in the POST array has been set. Checking for ’empty’ will return true even if a checkbox was checked, but it has an empty value.