PHP电子邮件联系表单显示错误

时间:2015-03-22 21:11:11

标签: php html

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

'请输入您的信息。'

除非我将其留空,否则不会显示名称错误消息和电子邮件错误消息。我试图在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;
}

2 个答案:

答案 0 :(得分:2)

在您的HTML表单中,您可以为<input...字段命名&#34;消息&#34;但是当你使用PHP时,你会尝试从$ _GET [&#39; comment&#39;]中获取值。

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

答案 1 :(得分:0)

我可以看到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?