[英]Second set of checkbox form data not populating to PHP form mail body?
我一直在解決表單中的問題,該表單的許多字段未通過PHP郵件表單正確填充。
我回到基礎知識,將其分解為幾個字段,並且在將第二組復選框數據添加到組合中時似乎遇到了問題。 正如下面復制的那樣,該電子郵件將發送,並且正文包括$ sport_msg之前的所有內容。 它不包括$ availability_msg或$ message,盡管它是在添加第二組復選框數據之前這樣做的。
我90%確信問題出在我$ email_body的語法上,但是我不確定自己做錯了什么。 或者,這可能與兩組復選框的“ foreach”部分中的括號有關。 誰能比我更擅長於PHP,請給我另一雙眼神?
我的PHP:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$company = $_POST['company'];
$visitor_email = $_POST['email'];
$sport = $_POST['sport'];
$message = $_POST['message'];
foreach($_POST['availability'] as $value) {
$availability_msg .= "$value\n";
}
foreach($_POST['sport'] as $value) {
$sport_msg .= "$value\n";
}
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'VGFV Guide Signup';//<== Shows up in the "from" field
$email_subject = "New Guide Signup";//<== Email Subject
//Begin Email Body - I BELIEVE THIS IS WHERE MY ISSUE IS!!!
$email_body = "$name. has signed up from $company\n\n".
"Here is the info:\n \n".
"Guide Services Offered: \n".$sport_msg;
"Availability: \n".$availability_msg;
"$message \n \n".
$to = "myemailaddress@mydomain.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
聲明變量,因為如果數組為空,則循環將不會對其進行設置:
$availability_msg = '';
$sport_msg = '';
$email_body
也存在語法錯誤,這是已修復的版本:
$email_body = "$name. has signed up from $company\n\n".
"Here is the info:\n \n".
"Guide Services Offered: \n".$sport_msg .
"Availability: \n".$availability_msg .
$message;
您是否檢查過值,例如
error_log(print_r($_POST,1));
error_log(print_r($_POST['availability'],1);
error_log(print_r($_POST['sport'] ,1));
您是否事先檢查了要傳遞給郵件函數的變量,例如(對所有變量都執行此操作):
error_log(" TO>".$to."<");
另外,郵件功能的返回值是多少?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.