【问题标题】:contents of text area not posting文本区域的内容未发布
【发布时间】:2013-02-13 07:39:25
【问题描述】:

我有一个位于https://pnrbuilder.com/_popups/feedback_popup.html 的表格

表单使用 post 将输入传递给 php 页面,该页面发送包含帖子内容的电子邮件,然后重定向用户。

输入字段工作正常,但 textarea 内容无法发送到电子邮件。

知道我做错了什么吗?

PHP 页面:

<?php
/*
This first bit sets the email address that you want the form to be submitted to.
You will need to change this value to a valid email address that you can access.
*/
$webmaster_email = "support@email.com";

/*
This bit sets the URLs of the supporting pages.
If you change the names of any of the pages, you will need to change the values here.
*/
$feedback_page = "feedback_form.html";
$error_page = "error_message.html";
$thankyou_page = "thank_you.html";


/*
This next bit loads the form field data into variables.
If you add a form field, you will need to add it here.
*/
$EmailAddress = $_POST['EmailAddress'] ;
$IssueType = $_POST['IssueType'] ;
$Comments = $_POST['Comments'] ;

/*
The following function checks for email injection.
Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
*/
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;
    }
}


// If email injection is detected, redirect to the error page.
if ( isInjected($EmailAddress) ) {
header( "Location: $error_page" );
}

// If we passed the previous test, send the email then redirect to the thank you page.
else {
mail( "$webmaster_email, test@email.com", "Feedback",
  $EmailAddress, $IssueType, $Comments );
header( "Location: $thankyou_page" );
}
?>

如果我将下面的内容放在我的 php 页面顶部,它会回显 textarea 的内容

echo $_POST["EmailAddress"];
echo $_POST["IssueType"];
echo $_POST["Comments"];

【问题讨论】:

  • 您能否尝试在通过电子邮件发送之前 输出$_POST 变量?他们没有出现吗?
  • 在上面编辑:是的,在发送电子邮件之前会显示文本区域
  • 请在下面查看我的答案。这应该为您解决问题。 ;-)

标签: php html forms post textarea


【解决方案1】:

请检查 PHP 的 mail 函数的正确形式:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

参考:http://php.net/manual/en/function.mail.php


消息应该是第三个参数,不是吗?

你写的是:

mail( "$webmaster_email, designedondemandcorp@gmail.com", "Feedback",
  $EmailAddress, $IssueType, $Comments );

像这样重写它:

$messageBody = "Comments : ".$Comments." Issue : ".$IssueType;
mail("$webmaster_email, designedondemandcorp@gmail.com", "Feedback", $messageBody);

只是想看看$_POST 是否有问题,但我想我上面的代码会解决这个问题(只需将你的所有数据捆绑在一个$messageBody var 中并将其传递给@987654328 @函数)。

【讨论】:

  • 感谢您对 DID 工作的帮助。最终我会选择 Jack 的答案,因为它以更易读的方式格式化电子邮件。
  • @DelightedD0D 很高兴能帮上忙,伙计! ;-)(旁注:我只是用一个快速的方法来展示它应该如何完成......)
【解决方案2】:

发送电子邮件

您将错误的参数传递给mail()

mail( "$webmaster_email, designedondemandcorp@gmail.com", "Feedback", $EmailAddress, $IssueType, $Comments);

应该是:

$contents = <<<EOM
Email: $emailAddress

Issue type: $IssueType

Comments:
$Comments
EOM;

mail( "$webmaster_email, designedondemandcorp@gmail.com", "Feedback", $contents);

电子邮件验证

其次,您应该使用正确的电子邮件验证:

$emailAddress = filter_input(INPUT_POST, 'EmailAddress', FILTER_VALIDATE_EMAIL);
if ($emailAddress === false) {
    header( "Location: $error_page" );
}

【讨论】:

  • 完美的邮件部分现在很好用,非常感谢顺便说一句,我尝试使用电子邮件验证,但是当 php 页面加载时我得到以下信息:Warning: filter_input() expects parameter 1 to be long, string given in /home/content/08/10125908/html/_popups/send_mail.php on line 51
  • 我将您帖子中的 emailAddress 更改为 EmailAddress 但我仍然遇到同样的错误
  • @DelightedD0D 抱歉,我错过了将 INPUT_POST 添加为第一个参数 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多