0

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.

13
  • 2
    Sidenote: Missing ; at $body = "Project Type:$projectType\n" and make sure your submit button is named submit but won't make it an answer, fearing it's a typo. Commented Feb 27, 2014 at 16:34
  • 1
    They usually open up "cans for worms" lol @meda Commented Feb 27, 2014 at 16:40
  • 2
    ^--« You see? @meda I knew it was a typo ;-) Commented Feb 27, 2014 at 16:45
  • 1
    @Fred-ii- Omg you were so right , I understand why you reacted this way now lol Commented Feb 27, 2014 at 16:50
  • 1
    @meda It's always best to comment before jumping to conclusions. Least, in my experiences ;-) Commented Feb 27, 2014 at 16:52

4 Answers 4

2

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";
Sign up to request clarification or add additional context in comments.

2 Comments

not so fast LOL! @meda
This worked great but you forgot to put a "," betweent the "glue" and the array. But it worked out great. Also they way you wrote it I thought it replaced $projectType = $_POST['projectType']; but actually the implode goes below.Some trial and error but it worked out great. thanks a lot!
2

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>';
    }
}
?>

2 Comments

good answer, I moved the variable inside of the condition to avoid undefined values
Thanks. sergio copied the implode
0
foreach($_POST['projectType'] as $value){
  echo $value;
}

1 Comment

where exactly would this go? Within the if ($_POST['submit']) { if (mail ($to, $subject, $body, $from)) { }
-1

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)

3 Comments

Actually you need the bracket
That's not related. Why would you want to remove the []s? This won't work without them.
It is if the op meant to use radio rather than checkbox as the class implies. There was no mention of wanting more than one type to be in the email body

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.