PHP $ _POST没有携带字段元素

时间:2012-03-29 18:54:40

标签: php forms post contact

我正在为客户制作联系表单,但出于某种原因,它并没有正确地执行变量。看一下代码,看看你能找到什么......我的想法已经不多了。

contact.php

<form name="contact" method="post" action="thankyou.php">
  <input type="hidden" name="flag" value="1" />
  <table style="width: 500px;">
  <tr>
    <td><p><em><strong>Your email: *</strong></em></p></td>
    <td><input name="email" type="text" size="30" /></td>
  </tr>
  <tr>
    <td><p><em><strong>Subject:</strong></em></p></td>
    <td><input name="subject" type="text" size="30" /></td>
  </tr>
  <tr>
    <td><p><em><strong>Message: *</strong></em></p></td>
    <td><textarea name="message" type="text" rows="7" cols="30"></textarea></td>
  </tr>
  <tr>
    <td></td>
    <td><div style="height:5px"></div></td>
  </tr>
  <tr>
    <td colspan="2"><p>To help protect us against spam, please type <em>storage</em> below: *</p></td>
  </tr>
  <tr>
    <td></td>
    <td><input name="spam_test" maxlength="7" type="text" size="5" /></td>
  </tr>
  <tr>
    <td></td>
    <td><div style="height:5px"></div></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="submit" value="Send" />
    <input type="reset" value="Reset" /></td>
  </tr>
  </table>
</form>

thankyou.php

<?php
  $user_message = "";
  if(isset($_POST['flag'])) {
    $flag = "true";
    if (strlen($_POST['message']) > 20000) {
      $user_message = "Message too long, please shorten.";
      $flag = "false";
    }
    if (strlen($_POST['message']) < 1) {
      $user_message = "Message cannot be blank.";
      $flag = "false";
    }
    if (strlen($_POST['subject']) > 5000) {
      $user_message = "Subject too long, please shorten.";
      $flag = "false";
    }
    if(!check_email($_POST['email'])) {
      $user_message = "Invalid email address, please re-enter.";
      $flag = "false";
    }
    if($_POST['spam_test'] != "storage") {
      $user_message = "Please fill out the spam prevention field correctly.";
      $flag = "false";
    }
    if($flag == "true") {
      $user_message = "Your message has been sent. We will be in touch shortly.";

      $headers = "From: ContactForm@example.com\r\n";
      $headers .= "Reply-To: " . $_POST['email'] . "\r\n";
      $headers .= "Return-Path: " . $_POST['email'] . "\r\n";

      $msg = "This is an automatically generated email from the Contact Form on mrstore.co.uk - below is a message from a user:\n\n";
      $msg .= "User's email:  " . $_POST['email'] . "\n\n";
      $msg .= "Message: " . $_POST['message'] . "\n";

      $subject = "Contact Us Message From Website: " . $_POST['subject'];

      if(!mail('user@example.org', $subject, $msg, $headers)) {
      $user_message = "Error Sending Email, please try again later.";
      }
    }
  }
?>
<h2><?php echo $user_message; ?></h2>

修改 以下是print_r($ _ POST)的结果...字段元素被转移到thankyou.php,但未正确处理(即示例中的垃圾邮件密钥错误)并且邮件未被发送。

Array ( [flag] => true [email] => user@example.org [subject] => This is the subject [message] => hello moto [spam_test] => blah )

编辑2 我觉得我越来越接近搞清楚,但现在它丢了500服务器错误。我还在thankyou.php中创建了变量$ email并将其指向$ _POST ['email'])这是函数check_email ...

function check_email($email) {
        // First, we check that there's one @ symbol, and that the lengths are right
        if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
            // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
            return false;
        }
        // Split it into sections to make life easier
        $email_array = explode("@", $email);
        $local_array = explode(".", $email_array[0]);
        for ($i = 0; $i < sizeof($local_array); $i++) {
            if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
                return false;
            }
        }
        if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
            $domain_array = explode(".", $email_array[1]);
            if (sizeof($domain_array) < 2) {
                return false; // Not enough parts to domain
            }
            for ($i = 0; $i < sizeof($domain_array); $i++) {
                if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
                    return false;
                }
            }
        }
        return true;
    }

最终编辑......我希望!! 我把它缩小到这个if语句......

if($flag == "true") {
                            $headers = "From: ContactForm@mrstore.co.uk\r\n";
                            $headers .= "Reply-To: " . $_POST['email'] . "\r\n";
                            $headers .= "Return-Path: " . $_POST['email'] . "\r\n";

                            $msg = "This is an automatically generated email from the Contact Form on mrstore.co.uk - below is a message from a user:\n\n";
                            $msg .= "User's email:  " . $_POST['email'] . "\n\n";
                            $msg .= "Message: " . $_POST['message'] . "\n";

                            $subject = "Contact Us Message From Website: " . $_POST['subject'];
                            mail("dgengler@ccseo.com", $subject, $msg, $headers);
                            $user_message = "Your message has been sent. We will be in touch shortly.";                }

2 个答案:

答案 0 :(得分:0)

我发现发送POST数据没有任何问题。

确保您的'check_email'功能存在。和您的电子邮件发送功能正常工作。

答案 1 :(得分:0)

尝试使用您的代码重新创建此代码时,我遇到的唯一问题就是使用此行:

if(!check_email($_POST['email'])) {

也许试试:

if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {

看看它是否有效。