非常感谢php新手的一些帮助!
HTML表单包含以下复选框列表:
<input type="checkbox" name="servicetype[]" value="Option A"> Option A<br>
<input type="checkbox" name="servicetype[]" value="Option B"> Option B<br>
<input type="checkbox" name="servicetype[]" value="Option C"> Option C<br>
<input type="checkbox" name="servicetype[]" value="Option D"> Option D<br>
<input type="checkbox" name="servicetype[]" value="Option E"> Option E<br>
下面的PHP代码无效。我希望表单上的勾选选项可以在电子邮件中发送。所有其他非复选框日期到达确定。这是php:
$mail = new Mail();
$mail->to = "my@emailadddress.com";
$mail->from = $_REQUEST["emailaddress"];
$mail->subject = "Form Results";
$mail->body.= "Name: ".$_REQUEST["title"]." ".$_REQUEST["firstname"]." ".$_REQUEST["surname"]. "\n";
$mail->body.= "Email Address: ".$_REQUEST["emailaddress"]. "\n";
$mail->body.= "Phone Number: ".$_REQUEST["telephonenumber"]. "\n";
$mail->body.= "Address: ".$_REQUEST["addressline1"]. "\n";
$mail->body.= $_REQUEST["addressline2"]. "\n";
$mail->body.= "Town/City: ".$_REQUEST["towncity"]. "\n";
$mail->body.= "County/State: ".$_REQUEST["countystate"]. "\n";
$mail->body.= "Post Code: ".$_REQUEST["postcode"]."\n";
$mail->body.= "Country: ".$_REQUEST["country"]."\n";
$servicetype = $_POST['servicetype'];
$body .= "servicetype: \r\n".
foreach ($servicetype as $selected) {
$body .= " > ".$selected."\r\n";
}
$mail->send();
$success=1;
php中的servicetype位是我复制的部分没有成功。任何人都可以帮忙吗?
答案 0 :(得分:1)
$body
而不是$mail->body
中,即它不会包含在您的邮件中。.
而不是;
$_POST
而不是$_REQUEST
。这应该无关紧要(假设您在表单中使用post
方法),但它很“奇怪”且容易出错,突然在两者之间切换。$servicetype
将被取消并且您会收到错误。使用isset()
进行此项检查。因此,请将代码的最后一部分更改为:
$servicetype = $_POST['servicetype'];
if(isset($servicetype)) {
$mail->body .= "servicetype: \r\n";
foreach ($servicetype as $selected) {
$mail->body .= " > ".$selected."\r\n";
}
}
答案 1 :(得分:1)
你可以这样做 -
$servicetype = implode(",", $_POST['servicetype']);
$mail->body .= $servicetype."\r\n";