php表单的错误

时间:2015-01-22 06:18:12

标签: php html forms

我正在为我的网站制作一份小型联系我们表格。即使我正确输入所有信息,我也没有收到邮件。

即使我正确地输入了所有细节,它也说有一些错误,没有任何反应。

有人可以帮我解决问题所在,我该如何解决?

HTML -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <body>
        <!-- The real form thing begins here -->
        <form name="feedback" method="post" action="feedback.php">
            <table width="450px">
                <tr>
                    <td valign="top">
                        <label for="first_name">First Name *</label>
                    </td>
                    <td valign="top">
                        <input  type="text" name="first_name" maxlength="50" size="30">
                    </td>
                </tr>
                <tr>
                    <td valign="top">
                        <label for="last_name">Last Name *</label>
                    </td>
                    <td valign="top">
                        <input  type="text" name="last_name" maxlength="50" size="30">
                    </td>
                </tr>
                <tr>
                    <td valign="top">
                        <label for="email">Email Address *</label>
                    </td>
                    <td valign="top">
                        <input  type="text" name="email" maxlength="80" size="30">
                    </td>
                </tr>
                <tr>
                    <td valign="top">
                        <label for="comments">Your Message for us *</label>
                    </td>
                    <td valign="top">
                        <textarea  name="message" maxlength="1000" cols="25" rows="6"></textarea>
                    </td>
                    <td>
                        <input type="submit" value="submit">
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

php -

<?php
 if(isset($_POST['email'])) {

 $email_to = "viadsindia@gmail.com";

 $email_subject = "Feedback from your website viads.in";


 function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
 }

 // validation expected data exists
 if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form.');       
 }

 $first_name = $_POST['first_name']; // required
 $last_name = $_POST['last_name']; // required
 $email_from = $_POST['email']; // required
 $comments = $_POST['comments']; // required

 $error_message = "";
 $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
 if(!preg_match($email_exp,$email_from)) {
 $error_message .= 'The Email Address you entered looks invalid.<br />';
 }
 $string_exp = "/^[A-Za-z .'-]+$/";
 if(!preg_match($string_exp,$first_name)) {
 $error_message .= 'The First Name you entered looks invalid.<br />';
 }
 if(!preg_match($string_exp,$last_name)) {
 $error_message .= 'The Last Name you entered looks invalid.<br />';
 }
 if(strlen($comments) < 2) {
 $error_message .= 'The Comments you entered looks invalid.<br />';
 }
 if(strlen($error_message) > 0) {
 died($error_message);
 }
 $email_message = "Form details below.\n\n";

 function clean_string($string) {
 $bad = array("content-type","bcc:","to:","cc:","href");
 return str_replace($bad,"",$string);
 }

 $email_message .= "First Name: ".clean_string($first_name)."\n";
 $email_message .= "Last Name: ".clean_string($last_name)."\n";
 $email_message .= "Email: ".clean_string($email_from)."\n";
 $email_message .= "Comments: ".clean_string($comments)."\n";


 // create email headers
 $headers = 'From: '.$email_from."\r\n".
 'Reply-To: '.$email_from."\r\n" .
 'X-Mailer: PHP/' . phpversion();
 @mail($email_to, $email_subject, $email_message, $headers);  
 ?>

 <!-- If everything goes well then display this message -->

 Thanks for contacting us! We will get back to you soon. your feedback is much appreciated!

 <?php
 }
 die();
 ?>

1 个答案:

答案 0 :(得分:1)

您的表单中没有comments字段!

<textarea  name="message"

将此更改为

<textarea  name="comments"