Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Question: HOW DO I DISPLAY SELECTED CHECKBOXES IN MY EMAIL?

I've created a survey (http://www.hello-rio.com/surveyonshoes/) and I'm trying to get my selected checkboxes to display in my email when the form is submitted. I'm a complete noob when it comes to this, any help would be greatly appreciated

Heres my html sample checkbox code (in index.html):

<label for="colors">What colors do you own? (check all that apply)</label>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="black">black </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="brown">brown </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="beige">beige </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="white">white </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="gold">gold </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="silver">silver </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="red">red </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="blue">blue </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="yellow">yellow </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="green">green </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="orange">orange </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="purple">purple </div><br>
<label for="others">Others</label>
<input type="text" name="color_flats[]" class="others" />

and heres the complete php code (contact.php):

<?php

/* Set e-mail recipient */
$myEmail  = "[email protected]";

/* Check all form inputs using check_input function */
$subject         = "Survey on Shoes";
$Name            = check_input($_POST['Name'], "Enter your name");
$Address         = check_input($_POST['Address']);
$Email           = check_input($_POST['Email']);
$Age             = check_input($_POST['Age']);
$Sex             = check_input($_POST['Sex']);
$Status          = check_input($_POST['Status']);
$Employment      = check_input($_POST['Employment']);
$Income          = check_input($_POST['Income']);
$pairs_flats     = check_input($_POST['pairs_flats']);
$color_flats     = check_input($_POST['color_flats']);
$size_flats      = check_input($_POST['size_flats']);
$material_flats  = check_input($_POST['material_flats']);
$brand_flats     = check_input($_POST['brand_flats']);
$frequency_flats = check_input($_POST['frequency_flats']);
$cost_flats      = check_input($_POST['cost_heels']);
$pairs_heels     = check_input($_POST['pairs_heels']);
$color_heels     = check_input($_POST['color_heels']);
$size_heels      = check_input($_POST['size_heels']);
$material_heels  = check_input($_POST['material_heels']);
brand_heels      = check_input($_POST['brand_heels']);
$frequency_heels = check_input($_POST['frequency_heels']);
$cost_heels      = check_input($_POST['cost_heels']);
$height_heels    = check_input($_POST['height_heels']);
$work            = check_input($_POST['work']);
$mall            = check_input($_POST['mall']);
$events          = check_input($_POST['events']);
$travel          = check_input($_POST['travel']);


/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $Email))
{
    show_error("E-mail address not valid");
}

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
    $website = '';
}

/* Let's prepare the message for the e-mail */
$message = "Hello!

Your form has been submitted by:

Name: $Name
Address: $Address
Email: $Email
Age: $Age
Sex: $Sex
Status: $Status
Employment: $Employment
Income: $Income


FLATS
Pairs: $pairs_flats pairs
Color: $check_msg
Size: $size_flats
Material: $material_flats
Brand: $brand_flats
Frequency: $frequency_flats pairs a year
Cost: Php $cost_flats


HEELS
Pairs: $pairs_heels pairs
Color: $color_heels
Size: $size_heels
Material: $material_heels
Brand: $brand_heels
Frequency: $frequency_heels pairs a year
Cost: $cost_heels
Height: $height_heels inches


Work/School: $work
Mall: $mall
Events: $events
Travel: $travel


End of message
";

/* Send the message using mail() function */
mail($myEmail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

/* Functions we used */
function check_input($data, $problem='')

    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
        return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

        <b>Please correct the following error:</b><br />
        <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>

I've tried all sorts of codes like implode, if statements, foreach etc.. cant seem to get it right...

share|improve this question
1  
There a error on this line brand_heels = check_input($_POST['brand_heels']); Missing $ at the start of the line. –  David Bélanger Jul 31 '12 at 15:16

2 Answers 2

up vote 0 down vote accepted
$color_flats = check_input(implode("," , $_POST['color_flats']));

$message = "...

FLATS
Pairs: $pairs_flats pairs
Color: $color_flats


...";
share|improve this answer
    
This is awesome! it solved my problem! Can't thank you enough, I've been stumbling on this code for a couple of days now. Thanks again –  Gian Aguilar Jul 31 '12 at 15:27

Since $color_flats is an array, you need to loop on each

foreach($color_flats as $whatever){
    echo $whatever;
}

or you can implode it to make one and unique string :

echo implode(',', $color_flats);

<?php

#   Default Vars
$_color_flats = '';
if(isset($color_flats) === TRUE){
    #   Is Array ?
    if(is_array($color_flats) === TRUE){
        #   Count
        $c = count($color_flats);

        #   Loop
        for($i=0; $i < $c; $i++){
            $_color_flats.= (isset($color_flats[$i]) === TRUE ? $color_flats[$i] : '').($i == ($c-1) ? '' : ($i == $c-2 ? ' and ' : ', '));
        }
    }
}

echo $_color_flats;

?>
share|improve this answer
    
Where exactly do I insert this code? Thanks –  Gian Aguilar Jul 31 '12 at 15:22
    
@GianAguilar Take the variable $_color_flats and echo it inside your message. –  David Bélanger Jul 31 '12 at 15:26
    
Thanks for your help! –  Gian Aguilar Jul 31 '12 at 15:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.