PHP没有获取POST信息

时间:2015-04-09 17:50:13

标签: php forms

我正在尝试使用PHP mail()函数通过电子邮件向我发送用户反馈。这是我的代码:

<form method="post" action="#" onsubmit="return false" name="form">
<table>   


<tr>
<td>Your email:</td> <td><input required type="text" name="youremail" placeholder="email@email.com"></td>
</tr>
<tr>
<td>Your message: </td><td><textarea rows="30" cols="40" name="message" style="vertical-align:top;"></textarea></td>
</tr>
<tr><td>
<input type="submit" name="submit">
</td></tr>
</table>
</form>


<?php

$to = "email@email.com";

$message = $_POST["message"];

mail($to, 'Feedback', $message);

?>

邮件已发送,但邮件正文中没有任何内容。我已对它进行了测试,但它无法正常工作。

4 个答案:

答案 0 :(得分:3)

如上所述。页面加载后,您的页面将立即发送空信息。

因此,最好使用isset()empty()

确保消息不为空且同时使用这两个条件:

if(isset($_POST['submit']) && !empty($_POST['message'])){

// ...

}

您还应该使用正确的邮件标题,否则许多系统会在没有From:

的情况下将邮件识别为垃圾邮件
  

如果不这样做,将导致类似于警告的错误消息:mail():&#34; sendmail_from&#34;未设置在php.ini或custom&#34; From:&#34;标头丢失。 From标头还设置了Windows下的Return-Path。

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

答案 1 :(得分:1)

你应该记录你的错误,你会看到:

  

注意:未定义的索引:第22行的xxxx中的消息

此外,您还没有发送表单:onsubmit="return false"

确保使用isset()功能检查是否也设置了变量。

答案 2 :(得分:0)

您必须提交表格。 然后,您可以检查变量是否存在并运行脚本。

尝试

if(isset($_POST["message"])){
  $to = "email@email.com";
  $message = $_POST["message"];
  mail($to, 'Feedback', $message);
}

删除onsubmit =&#34;返回false&#34; 使用onsubmit =&#34;返回false&#34;表格无法发送。

<form method="post" action="#" name="form">
<table>   


<tr>
<td>Your email:</td> <td><input required type="text" name="youremail" placeholder="email@email.com"></td>
</tr>
<tr>
<td>Your message: </td><td><textarea rows="30" cols="40" name="message" style="vertical-align:top;"></textarea></td>
</tr>
<tr><td>
<input type="submit" name="submit">
</td></tr>
</table>

答案 3 :(得分:0)

您可以检查它是否是POST:

<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
         $to = "email@email.com";

         $message = $_POST["message"];

         mail($to, 'Feedback', $message);
    } 
?>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" name="form">

    <table>   


    <tr>
    <td>Your email:</td> <td><input required type="text" name="youremail" placeholder="email@email.com"></td>
    </tr>
    <tr>
    <td>Your message: </td><td><textarea rows="30" cols="40" name="message" style="vertical-align:top;"></textarea></td>
    </tr>
    <tr><td>
    <input type="submit" name="submit">
    </td></tr>
    </table>
    </form>