markyeoj
markyeoj

Reputation: 369

PHP Email Send value of checkbox Yes if checked No if Unchecked

Hello I am creating a HTML + PHP email form. My form and function is working perfectly aside from my checkboxes. I have two checkboxes.

<input type="checkbox" id="boxfan" name="boxfan[]"/>
<label for="boxfan">A fan?</label>

<input type="checkbox" id="boxgbps" name="boxfan[]"/>
<label for="boxalbum">Bought an album?</label>

I want the value to be send as Yes or No, send Yes if the checked and No if unchecked.

For example:

A fan? Yes

Bought an album? No

Any, suggestion is appreciated. Thanks.

My actual form is here It's in Danish that's why I just used a simple example.

Upvotes: 1

Views: 3222

Answers (2)

user3130221
user3130221

Reputation:

If you want to send each in a seperate email:

foreach($_POST['boxfan'] as $boxfan){
    if(isset($boxfan)){
        $message = "Yes";
        mail($to, $subject, $message);
    } else{
        $message = "No";
        mail($to, $subject, $message);
    }
}

But if you want to send both in one email:

$message = null;
foreach($_POST['boxfan'] as $boxfan){
    if(isset($boxfan)){
        $message .= "Yes\r\n";
    } else{
        $message .= "No\r\n";
    }
}
mail($to, $subject, $message);

Upvotes: 3

Leonid Shevtsov
Leonid Shevtsov

Reputation: 14179

This is how Rails does it:

<input type="hidden" name="boxfan" value="No"/>
<input type="checkbox" id="boxgbps" name="boxfan" value="Yes"/>

[EDIT] but you won't be able to make it an array variable then.

Upvotes: 0

Related Questions