简体   繁体   English

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

[英]PHP email contact form displays error

Every time I attempt to submit this contact form I receive the following error message: 每次尝试提交此联系表时,都会收到以下错误消息:

'Please enter your message.' “请输入您的信息。”

The name error message and email error message do not appear unless I leave them blank. 除非我将其保留为空白,否则不会出现名称错误消息和电子邮件错误消息。 I attempted specifying post in the HTML. 我尝试在HTML中指定发布。

Here is the 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>

Here is the PHP: 这是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;
}

In your HTML form you name your <input... field "message" but then when you are in PHP you try to get the value from `$_GET['comment']. 在HTML表单中,您将<input...字段命名为“ message”,但是当您使用PHP时,尝试从`$ _GET ['comment']获取值。

I think if you get those lined up I think it will solve your problem. 我认为,如果您将这些排队,那么我认为它将解决您的问题。

I can see 2 issues: 我可以看到2个问题:

  1. Receive $_GET['comment'] or $_POST['comment'] but next you're using $message var 接收$ _GET ['comment']或$ _POST ['comment'],但接下来您将使用$ message var
  2. Do not use if($_POST) because $_POST as a superglobal is always setted. 不要使用if($ _ POST),因为始终将$ _POST设置为超全局变量。

I suggest use this for check if a POST have been sent 我建议使用它来检查是否已发送POST

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

or this in case you want to check is not empty 或者如果您要检查的内容不为空

if ( !empty($_POST) ) {}

How to detect if $_POST is set? 如何检测是否设置了$ _POST?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM