PHP $ _POST没有携带字段元素

时间:2022-09-20 11:13:29

I'm working on a contact form for a client, but for some reason, it's not carrying over the variables right. Take a look at a bit of the code and see if you can find anything..I'm running out of ideas.

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

contact.php

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

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>

EDIT The following is the result of print_r($_POST)... Field elements are being carried over to thankyou.php, but are not being processed correctly (ie. spam key is wrong in the example) and the mail is not being sent.

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

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

EDIT 2 I feel like I'm getting closer to figuring it out, but now it's throwing a 500 server error. I also created the variable $email in thankyou.php and pointed it to $_POST['email']) Here's the function, check_email...

编辑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;
    }

FINAL EDIT...I HOPE!! I've narrowed it down to this if statement...

最后的编辑...我希望!我把它缩小到这个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 个解决方案

#1


0  

I couldn't find any problem to send POST data.

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

Make sure your 'check_email' function is exists. and your email sending function working properly.

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

#2


0  

When trying to recreate this using your code, the only issue I had was with this line:

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

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

Maybe try:

也许试试:

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

And see if it works.

看看它是否有效。

#1


0  

I couldn't find any problem to send POST data.

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

Make sure your 'check_email' function is exists. and your email sending function working properly.

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

#2


0  

When trying to recreate this using your code, the only issue I had was with this line:

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

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

Maybe try:

也许试试:

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

And see if it works.

看看它是否有效。