所以我有一个在线表格,如果每个字段都填满,或者你填写表格的90%,但如果你填写6/7字段并留下第二个字段为空,那么其余的字段不通过电子邮件发送。我一直在谷歌搜索几个小时,并不认为我已经搜索了正确的问题。这是实际发送电子邮件的代码:
$response = array();
$error = false;
$message = '';
if(empty($_POST) && !isset($_POST['send']))
{
$response['message'] = 'You have to submit the contact form first.';
$error = true;
}else{
unset($_POST['send']);
foreach($_POST as $key=>$val)
{
if($val == '')
{
$error = false;
$response['message'] = 'All fields are required';
break;
}
$message .= '<p>' . htmlspecialchars($key) . ': ' . htmlspecialchars($val) . '</p>';
}
}
if(!$error)
{
$subject = 'New message from Aguettant contact form';
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=iso-8859-1";
$headers[] = "From: contact@biosyent.com";
$headers[] = "Reply-To: {$_POST['Email']}";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
if(mail('xxxx@aol.io',$subject,$message,implode("\r\n", $headers)) !== FALSE)
{
// redirect
header("Location: thankyou.php");
die();
}else{
$response['message'] = 'Something went wrong. Please try again.';
}
我已经看出它与else / foreach有关,但如果我发表评论,我会收到一封没有内容的电子邮件。
答案 0 :(得分:1)
如果有一个空字段,则会中断循环并停止追加字段。
foreach($_POST as $key=>$val)
{
if($val == '')
{
$error = false;
$response['message'] = 'All fields are required';
break; // <-- here
}
$message .= '<p>' . htmlspecialchars($key) . ': ' . htmlspecialchars($val) . '</p>';
}
如果删除该中断,它应该按预期工作。