我是php的新手,所以很难从现有的答案中找出这个。
我需要查看生成的通知电子邮件中表单上最多选择了4个复选框中的哪一个。
表单是发送电子邮件,但它只包含发件人的评论,而不是复选框选项。
有人愿意在我的代码中指出错误吗?请继续并假设n00bness的最低理解水平。
以下是相关表格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/>
..这是我到目前为止拼凑的PHP脚本:
<?php
$email_to = "me@mysite.com";
$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");
}
?>
答案 0 :(得分:1)
问题是$ times是一个数组。你应该这样做:
$times = $_POST["timeslots"];
$times = implode(', ', $times);
然后您可以在电子邮件中使用它
$ times是一个数组,因为在PHP中,当您使用数组声明输入元素作为名称时(就像您所做的那样),会发布一个数组。在您的情况下,只会发布所选的复选框。
还有一件事:只发布复选框的值,所以如果你检查前两个复选框,你将在邮件中发送“thu,fri”。
答案 1 :(得分:1)
$ times是代码中的数组:
<?php
$email_to = "me@mysite.com";
$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");
}
?>
编辑:在原始代码行17&amp; 18应该是数组,(原始代码中的第18行未使用)