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

PHP电子邮件联系人表单显示错误,php,html,Php,Html,每次我尝试提交此联系人表单时,都会收到以下错误消息: “请输入您的消息。” 名称错误消息和电子邮件错误消息不会出现,除非我将其留空。我试图在HTML中指定post 以下是HTML: <div class="col-md-8 animated fadeInLeft notransition"> <h1 class="smalltitle"> <span>Get in Touch</span> </

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

“请输入您的消息。”

名称错误消息和电子邮件错误消息不会出现,除非我将其留空。我试图在HTML中指定post

以下是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;
}

在HTML表单中,您可以命名您的
我可以看到两个问题:

  • 接收$\u GET['comment']或$\u POST['comment'],但接下来您将使用$message var
  • 不要使用if($\u POST),因为$\u POST是超全局的 他总是坐着
  • 我建议用这个来检查邮件是否已经发送

    if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {}
    
    或者,如果要检查,则此字段不为空

    if ( !empty($_POST) ) {}