【问题标题】:PHP email contact form displays errorPHP 电子邮件联系表单显示错误
【发布时间】:2015-03-22 21:11:11
【问题描述】:

每次我尝试提交此联系表时,都会收到以下错误消息:

'请输入您的信息。'

除非我将它们留空,否则不会出现名称错误消息和电子邮件错误消息。我尝试在 HTML 中指定帖子。

这里是 HTML:

<div class="col-md-8 animated fadeInLeft notransition">
        <h1 class="smalltitle">
        <span>Get in Touch</span>
        </h1>
        <form action="contact.php" method="post" name="MYFORM" id="MYFORM">
            <input name="name" size="30" type="text" id="name" class="col-md-6 leftradius" placeholder="Your Name">
            <input name="email" size="30" type="text" id="email" class="col-md-6 rightradius" placeholder="E-mail Address">
            <textarea id="message" name="message" class="col-md-12 allradius" placeholder="Message" rows="9"></textarea>
            <img src="contact/refresh.jpg" width="25" alt="" id="refresh"/><img src="contact/get_captcha.php" alt="" id="captcha"/>
            <br/><input name="code" type="text" id="code" placeholder="Enter Captcha" class="top10">
            <br/>
            <input value="Send" type="submit" id="Send" class="btn btn-default btn-md">
        </form>
    </div>

这里是 PHP:

<?php

//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];

//flag to indicate which method it uses. If POST set it to 1

if ($_POST) $post=1;

//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$comment) $errors[count($errors)] = 'Please enter your message.'; 

//if the errors array is empty, send the mail
if (!$errors) {

    //recipient - replace your email here
    $to = 'faasdfsdfs@gmail.com';   
    //sender - from the form
    $from = $name . ' <' . $email . '>';

    //subject and the html message
    $subject = 'Message from ' . $name; 
    $message = 'Name: ' . $name . '<br/><br/>
               Email: ' . $email . '<br/><br/>      
               Message: ' . nl2br($comment) . '<br/>';

    //send the mail
    $result = sendmail($to, $subject, $message, $from);

    //if POST was used, display the message straight away
    if ($_POST) {
        if ($result) echo 'Thank you! We have received your message.';
        else echo 'Sorry, unexpected error. Please try again later';

    //else if GET was used, return the boolean value so that 
    //ajax script can react accordingly
    //1 means success, 0 means failed
    } else {
        echo $result;   
    }

//if the errors array has values
} else {
    //display the errors message
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
    echo '<a href="index.html">Back</a>';
    exit;
}


//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";

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

    if ($result) return 1;
    else return 0;
}

【问题讨论】:

    标签: php html


    【解决方案1】:

    在您的 HTML 表单中,您将 &lt;input... 字段命名为“消息”,但是当您使用 PHP 时,您会尝试从 `$_GET['comment'] 获取值。

    我想如果你把这些排成一列,我认为它会解决你的问题。

    【讨论】:

      【解决方案2】:

      我可以看到 2 个问题:

      1. 接收 $_GET['comment'] 或 $_POST['comment'] 但接下来您将使用 $message var
      2. 不要使用 if($_POST) 因为 $_POST 作为超全局 始终设置。

      我建议使用它来检查是否已发送 POST

      if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {}
      

      或者这个如果你想检查不为空

      if ( !empty($_POST) ) {}
      

      How to detect if $_POST is set?

      【讨论】:

        猜你喜欢
        • 2019-01-30
        • 2014-06-09
        • 2019-09-04
        • 1970-01-01
        • 2017-11-21
        • 2015-08-13
        • 2018-02-24
        • 1970-01-01
        • 2013-11-15
        相关资源
        最近更新 更多