1

I have implemented a form that sends submitted information to my email successfully. I can achieve this with all inputs except for checkboxes. What do I have to do in order for checkbox values to appear in my email as well? I have tried to use a for each loop but I dont know if I am along the right lines. Relevant code below:

Code:

$companyname = $_POST['companyname'];
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phonenumber = $_POST['phonenumber'];

foreach($_POST['check'] as $value) {
$check_msg .= "Checked: $value\n";
}

$to = 'woodsy0130@hotmail.com';
$subject = 'Quote';
$message = 'Company Name: '.$companyname. "\n"."\n". 'Full Name: ' .$fullname.     "\n"."\n". 'E-mail: '.$email. "\n"."\n". 'Phone Number: '.$phonenumber. "\n"."\n". 'Features: '$check_msg;

HTML:

<label><input type="checkbox" name="check[]" id="q12_1" value="E commerce  Shopping Cart" />E-commerce (Shopping Cart)</label>
<label><input type="checkbox" name="check[]" id="q12_2" value="Content Management System  CMS"   />Content Management System (CMS)</label>
user1278496
  • 1,056
  • 6
  • 17
  • 34
  • 1
    You didn't include the `$check_msg` in `$message` . Also, use `!empty`. http://stackoverflow.com/questions/2268887/php-checkbox-input – Vucko Dec 12 '12 at 14:34
  • Sorry, I did include it, I just forgot to add it. See revision - still doesnt work... – user1278496 Dec 12 '12 at 14:37
  • 1
    `$check_msg .= "Checked:".$value."\n";` – Vucko Dec 12 '12 at 14:38
  • Assuming that you haven't accidentally placed the checkboxes outside your form, what does the $_POST['check'] variable hold (use a var_dump, perhaps)? – Gareth Cornish Dec 12 '12 at 14:46

2 Answers2

3

Try:

$message = 'Company Name: '.$companyname. "\n"."\n". 'Full Name: ' .$fullname. "\n"."\n". 'E-mail: '.$email. "\n"."\n". 'Phone Number: '.$phonenumber. "\n"."\n". 'Features: '.$check_msg;

You were missing the . after 'Features: '

For example, this works:

<?php

$companyname = $_POST['companyname'];
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phonenumber = $_POST['phonenumber'];

foreach ($_POST['check'] as $value) {
    $check_msg .= "Checked: $value\n";
}

$to = 'woodsy0130@hotmail.com';
$subject = 'Quote';
$message = 'Company Name: '.$companyname. "\n"."\n". 'Full Name: ' .$fullname.     "\n"."\n". 'E-mail: '.$email. "\n"."\n". 'Phone Number: '.$phonenumber. "\n"."\n". 'Features: '.$check_msg;
echo $message;
?>
<form action='' method="post">
    <label>
        <input type="checkbox" name="check[]" id="q12_1" value="E commerce  Shopping Cart" />
        E-commerce (Shopping Cart)</label>
    <label>
        <input type="checkbox" name="check[]" id="q12_2" value="Content Management System  CMS"   />
        Content Management System (CMS)</label>
    <input type="submit" value="submit">
</form>
SeanWM
  • 16,789
  • 7
  • 51
  • 83
1

Try like this.

if(!empty($_POST['check'])) {
    foreach($_POST['check'] as $value) {
        $check_msg .= "Checked:".$value."\n";            
    }}
Vucko
  • 20,555
  • 10
  • 56
  • 107