WeebleWobb
WeebleWobb

Reputation: 179

Checkbox Value not showing in Email

So I have created a simple form for registrations. Everything works perfectly except for the check boxes. They do not seem to show in the E-mail that is sent to the recipient.

Here is the HTML form section:

<input type="checkbox" class="radio" name="projectType[]" value="Logo/Identity System" required/>Logo/Identity System<br />
<input type="checkbox" class="radio" name="projectType[]" value="Poster" required/>Poster<br />
<input type="checkbox" class="radio" name="projectType[]" value="Brochure" required/>Brochure<br />

<input type="checkbox" class="radio" name="projectType[]" value="Ad Campaign" required/>Ad Campaign<br />
<input type="checkbox" class="radio" name="projectType[]" value="Website" required/>Website<br />
<input type="checkbox" class="radio" name="projectType[]" value="Other" required />Other<br />

Here is the PHP section:

<?php
$to = '[email protected]';
$projectType = $_POST['projectType'];
$body = "Project Type:$projectType\n"
if ($_POST['submit']) {
    if (mail ($to, $body)) {
        echo '<p>You have successfully registered for 2014 Designathon!</p>';
    } else {
        echo '<p>Something went wrong, go back and try again!</p>';
    }
}
?>

When the button submit is pressed all the information is sent but the "Project Type:" comes in empty with no value. How can I get it to show the values of the checkboxes selected? Don't mind the class "radio" They were previously radio buttons which worked fine but the client wanted multiple selections.

Upvotes: 0

Views: 1149

Answers (4)

sergio
sergio

Reputation: 5260

You try to send to email an array, but need a string. Simple change your code from :

$body = "Project Type:$projectType\n"

to:

$types = implode(', ', $projectType);
$body = "Project Type:$types\n";

Upvotes: 2

Shlomo
Shlomo

Reputation: 3990

List the checkbox values using implode() like that:

<?php
if ($_POST['submit']) {

  $to = '[email protected]';
  $projectType = $_POST['projectType'];
  $body = "Project Type: " . implode(", ", $projectType) ."\n";

    if (mail ($to, $body)) {
        echo '<p>You have successfully registered for 2014 Designathon!</p>';
    } else {
        echo '<p>Something went wrong, go back and try again!</p>';
    }
}
?>

Upvotes: 2

ATechGuy
ATechGuy

Reputation: 1270

foreach($_POST['projectType'] as $value){
  echo $value;
}

Upvotes: 0

Alex
Alex

Reputation: 7374

You've set project type as an array ( the [] ). Remove the square brackets and it should work as expected. I'd have thought "Array" would be passed though.

<input type="checkbox" class="radio" name="projectType" value="Logo/Identity System" required/>Logo/Identity System

Or, see the example by keaner, which loops through all potential values (if more than one can be selected)

Upvotes: -1

Related Questions