0

我想在我的网站上做一个联系表格,当我点击提交按钮时,它没有通过验证功能。它说表格似乎有问题,但我正在填写所有数据。我只是想知道我是否编码错误。大家可以看看吗?

PHP:

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

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email@website.com";
$email_subject = "";


function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    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['subject']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you   submitted.');       
}


$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$subject = $_POST['subject']; // not 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 does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<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 .= "Subject: ".clean_string($subject)."\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);  
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

HTML:

<form name="contactform" action="contactform.php" method="post" class="well span11">
    <div class="row">
            <div class="span3">
                <label for="first_name">First Name</label>
                <input type="text" name="first_name" class="span3" placeholder="Your First Name" required>
                <label for="Last_name">Last Name</label>
                <input type="text" name="Last_name" class="span3" placeholder="Your Last Name" required>
                <label for="email">Email Address</label>
                <input type="email" name="email" class="span3" placeholder="Your email address" required>
                <label for="subject">Subject</label>
                <select id="subject" name="subject" class="span3">
                    <option value="na" selected="">Choose One:</option>
                    <option value="service">Good Feedback</option>
                    <option value="suggestions">Poor Feedback</option>
                </select>
            </div>
            <div class="span8">
                <label for="comments">Message</label>
                <textarea name="message" id="comments" class="input-xlarge span8" rows="12"></textarea>
            </div>

            <button type="submit" value="Send" class="btn btn-primary pull-right">Send</button>
        </div>
    </form>

我还想我应该提到,如果我尝试注释掉验证部分,我会收到一个内部服务器错误(错误 500),这就是为什么我认为我的代码有问题。

4

2 回答 2

2
<textarea name="message" id="comments" class="input-xlarge span8" rows="12"></textarea>

您在 php 中期待 $_POST['comments'] 但在这里您发送 name="message"

另一件事:

$_POST['last_name']

<input type="text" name="Last_name" class="span3" placeholder="Your Last Name" required>

在 php 中,“姓氏”!=“姓氏”

于 2013-05-31T01:22:16.103 回答
0

当您检查是否一切都设置好时,大约在第 27 行左右,它说

died('We are sorry, but there appears to be a problem with the form you submitted.');

你确定它不应该是

die(We are sorry, but there appears to be a problem with the form you submitted.');

(死而不是死)

于 2013-05-31T01:16:40.690 回答