我在使用php邮件功能发送html表单的数据方面遇到了一些问题。邮件功能正常工作,我提交表单时收到电子邮件没有问题,但有时我的电子邮件中有空数组。
代码如下:
<?php
error_reporting(E_ALL & ~E_NOTICE);
$email = $_POST['email'];
//check if value is set:
if (isset($_POST['submit'])){
$everyoneToppingList = substr(implode(', ', $_POST['everyone_platter_topping']), 0);
$everyoneCondimentList = substr(implode(', ', $_POST['everyone_platter_condiment']), 0);
}; //end isset($_POST['submit']
$len = strlen($email);
if ($len > 0)
{
$email_body = "Full Name: $title $fullname\n".
"Topping? - $everyoneToppingList\n".
"Condiment? - $everyoneCondimentList\n\n";
$email_to = "example@gmail.com";
$email_from = $_POST['email']; // required
$URL= "order.php";
header ("Location: $URL");
// create email headers
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email_from \r\n";
$headers .= "Cc: $email_from \r\n";
$headers .= "X-Mailer: PHP/". phpversion();
mail($email_to, $email_subject, $email_body, $headers);
} ?>
html部分看起来像:
<table style="vertical-align:top;" class="form platter bread" cellspacing="2">
<tr>
<span class="bold">
Choose Your Topping:
</span>
<span id="errorsDiv_everyone_platter_topping[]">
</span>
<td height="7" width="87" align="left" valign="middle">
<input type="checkbox" name="everyone_platter_topping[]" value="Lettuce" id="everyone_lettuce" />
<label for="everyone_lettuce">\
Lettuce
</label>
</td>
<td height="7" width="87" align="left" valign="middle">
<input type="checkbox" name="everyone_platter_topping[]" value="Tomatoes" id="everyone_tomatoes" />
<label for="everyone_tomatoes">
Tomatoes
</label>
</td>
</tr>
</table>
当我收到电子邮件时,有时它只显示“Topping? - ,,,,”。
真的需要这方面的帮助,任何建议都将不胜感激!!
谢谢!
答案 0 :(得分:1)
我很抱歉在这里写下来,但是评论部分不足以提及对你的问题的怀疑。
$URL= "order.php";
header ("Location: $URL");
没有任何意义,它会重定向,邮件也不会被发送如果我对
进行微妙的更改header
代码email
输入框和submit
提交按钮POST
我只需通过上述更改就可以轻松完成此代码。
我建议您编辑一个问题,然后再次询问或进行建议的更改(当然还有一些调整),您将编码处于工作状态。
答案 1 :(得分:-1)
首先在使用em之前初始化变量。
$everyoneToppingList = null;
$everyoneCondimentList = null;
if (isset($_POST['submit'])){
$everyoneToppingList = substr(implode(', ', $_POST['everyone_platter_topping']), 0);
$everyoneCondimentList = substr(implode(', ', $_POST['everyone_platter_condiment']), 0);
};
$len = strlen($email);
if ($len > 0)
{
if (!is_null($everyoneToppingList) && !is_null($everyoneCondimentList))
{
// Send your mail.
}
}