0

I'm brand new to php so struggling to figure this one out from existing answers.

I need to see which of up to 4 checkboxes have been selected on a form in the resulting notification email.

The form IS sending the email, but it only includes the sender's comments, NOT the checkbox selection(s) made.

Anyone willing to point out the error in my code? Please go ahead and assume the lowest comprehension level of n00bness.

Here's the relevant form html:

    <input type="checkbox" name="timeslots[]" value="thu" />Thursday after 7pm <br/>
    <input type="checkbox" name="timeslots[]" value="fri" />Friday after 5.30pm <br/>
    <input type="checkbox" name="timeslots[]" value="sat" />Saturday afternoon<br/>
    <input type="checkbox" name="timeslots[]" value="sun" />Sunday afternoon<br/>

..and here's the php script I've cobbled together so far:

<?php
$email_to = "[email protected]";
$name = $_POST["name"];
$email = $_POST["email"];
$comments = $_POST["comments"];
$email_from = $_POST["email"];
$email_subject = "Form request";
$times = $_REQUEST["timeslots"];

if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
    // Invalid email address
    die("The email address entered is invalid.");
}


$headers =
"From: $email_from .\n";
"Reply-To: $email_from .\n";

$body = "Name: $name\n  Message: $comments\n 
  $times";

ini_set("sendmail_from",$email_from);
$sent=mail($email_to,$email_subject,$comments,$headers,"-f".$email_from);
if($sent)
{
header("Location:thanks.html");
}else{
header("Location:senderror.html");
}
?>

2 Answers 2

1

The problem is that $times is an array. you should do:

$times = $_POST["timeslots"];

$times = implode(', ', $times);

and then you can use it in your email

$times is an array because in PHP when you declare input elements with an array as name (like you did), an array is posted. In your case only the selected checkboxes will be posted.

One more thing: only the value of the checkbox will be posted, so if you check the first two checkboxes you will send "thu, fri" in the mail.

Sign up to request clarification or add additional context in comments.

5 Comments

That's the outcome I'm looking for, but still no dice (i.e. email sends and includes comments but nothing else in body). Does the positioning of those lines of code matter?
try to do var_dump($_POST);var_dump($times);die(); after you do the implode: what do you see on screen?
OK, adding that after the implode leads to this message on submitting the form: array(4) { ["timeslots"]=> array(4) { [0]=> string(5) "thurs" [1]=> string(3) "fri" [2]=> string(3) "sat" [3]=> string(3) "sun" } ["name"]=> string(8) "Jane Doe" ["email"]=> string(13) "[email protected]" ["comments"]=> string(4) "hkjh" } string(17) "thurs,fri,sat,sun"
this means that the variables are ok and they should appear in your mail: try tsending in the body just $times: $body = "$times";
Perfect - I wasn't referring to the $body variable properly, but it's fixed now. Thanks!
1

$times is array in your code:

<?php
$email_to = "[email protected]";
$name = $_POST["name"];
$email = $_POST["email"];
$comments = $_POST["comments"];
$email_from = $_POST["email"];
$email_subject = "Form request";
$times = $_POST["timeslots"];

if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
    // Invalid email address
    die("The email address entered is invalid.");
}

$strTimes = implode(", ", $times);

$headers[] = "From: $email_from .\n";
$headers[] = "Reply-To: $email_from .\n";

$body = "Name: $name\n  Message: $comments\n $strTimes";

ini_set("sendmail_from",$email_from);
$sent=mail($email_to,$email_subject,$comments,$headers,"-f".$email_from);
if($sent)
{
header("Location:thanks.html");
}else{
header("Location:senderror.html");
}
?>

Edited: in your original code line 17 & 18 should be arrays, (line 18 in your original code is unused)

2 Comments

Thanks - tried this version, but getting an error on send: Parse error: syntax error, unexpected T_VARIABLE in E:\Domains\z\mysite.com\steps.php on line 18 It's referring to the line: $headers = ..which is weird?
@Sharb you had a mistake in your original code, line 18 was unused, fixed :)

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.