【问题标题】:Second set of checkbox form data not populating to PHP form mail body?第二组复选框表单数据未填充到 PHP 表单邮件正文?
【发布时间】:2015-05-20 08:32:14
【问题描述】:

我一直在对一个表单的问题进行故障排除,该表单有很多字段无法通过 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;
  }
}

?>

【问题讨论】:

    标签: php html forms checkbox


    【解决方案1】:

    声明变量,因为如果数组为空,循环不会设置它们:

    $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;
    

    【讨论】:

    • 你肯定会。我不得不将引号移到 .$sport_msg 和 .$availability_msg 之外。您建议如何设置变量?
    • 要么使用我在答案中输入的前两行,要么你可以完全这样做:$availability_msg = join("\n", $_POST['availability']) . "\n"; $sport_msg = join("\n", $_POST['sport']) . "\n";
    【解决方案2】:

    您是否检查过这些值,例如

    error_log(print_r($_POST,1));
    error_log(print_r($_POST['availability'],1);
    error_log(print_r($_POST['sport'] ,1));
    

    您是否事先检查过要传递给邮件函数的变量,例如(对所有变量都这样做):

    error_log(" TO>".$to."<");
    

    另外,mail函数的返回值是多少?

    【讨论】:

      猜你喜欢
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      • 1970-01-01
      • 2011-08-17
      • 2018-02-27
      • 2020-01-04
      • 1970-01-01
      相关资源
      最近更新 更多