简单PHP';联系我们';形式

简单PHP';联系我们';形式,php,html,contact-form,materialize,Php,Html,Contact Form,Materialize,我正在使用materialize中的一些CSS和一些PHP构建一个“联系我们”表单。这是我到目前为止的代码 HTML 名字 姓 电子邮件 评论 提交 我的PHP <html> <head> <title>Thanks For Contacting Us</title> </head> <body> <?php $recipient = 'admin@example

我正在使用materialize中的一些CSS和一些PHP构建一个“联系我们”表单。这是我到目前为止的代码

HTML


名字
姓
电子邮件
评论
提交
我的PHP

<html>
    <head>
    <title>Thanks For Contacting Us</title>
    </head>
    <body>
    <?php
      $recipient = 'admin@example.com';
      $email = $_POST['email'];
      $realFirstName = $_POST['realfirstname'];
      $realSecondName = $_POST['realsecondname'];
      $subject = $_POST['comments'];
      # We'll make a list of error messages in an array
      $messages = array();
    if (!preg_match("/^[\w\+\-.~]+\@[\-\w\.\!]+$/", $email)) {
    $messages[] = "That is not a valid email address.";
    }
    if (!preg_match("/^[\w\ \+\-\'\"]+$/", $realName)) {
    $messages[] = "The real name field must contain only " .
    "alphabetical characters, numbers, spaces, and " .
    "reasonable punctuation. We apologize for any inconvenience.";
    }
    $subject = preg_replace('/\s+/', ' ', $subject);
    # Make sure the subject isn't blank afterwards!
    if (preg_match('/^\s*$/', $subject)) {
    $messages[] = "Please specify a subject for your message.";
    }

    $body = $_POST['body'];
    # Make sure the message has a body
    if (preg_match('/^\s*$/', $body)) {
    $messages[] = "Your message was blank. Did you mean to say " .
    "something?"; 
    }
      if (count($messages)) {
        foreach ($messages as $message) {
          echo("<p>$message</p>\n");
        }
        echo("<p>Click the back button and correct the problems. " .
          "Then click Send Your Message again.</p>");
      } else {
    mail($recipient,
          $subject,
          $body,
          "From: $realName <$email>\r\n" .
          "Reply-To: $realName <$email>\r\n"); 
        echo("<p>Your message has been sent. Thank you!</p>\n");
      }
    ?>
    </body>
    </html>

谢谢联系我们

首先,您要分配$realFirstName和$realSecondName,但您使用的正则表达式带有一个名为$realName的变量,我在代码中的其他任何地方都看不到这个变量

检查$body时也会遇到同样的问题,因为$u POST['body']不在表单中的任何位置


出于同样的原因,您的脚本会出现许多未定义的变量错误。

查看此
$\u POST['body']
,查看表单中没有的内容。所以,帮自己一个忙,学习如何调试。因为当你加载页面时,
$\u POST
没有被填充?
<html>
    <head>
    <title>Thanks For Contacting Us</title>
    </head>
    <body>
    <?php
      $recipient = 'admin@example.com';
      $email = $_POST['email'];
      $realFirstName = $_POST['realfirstname'];
      $realSecondName = $_POST['realsecondname'];
      $subject = $_POST['comments'];
      # We'll make a list of error messages in an array
      $messages = array();
    if (!preg_match("/^[\w\+\-.~]+\@[\-\w\.\!]+$/", $email)) {
    $messages[] = "That is not a valid email address.";
    }
    if (!preg_match("/^[\w\ \+\-\'\"]+$/", $realName)) {
    $messages[] = "The real name field must contain only " .
    "alphabetical characters, numbers, spaces, and " .
    "reasonable punctuation. We apologize for any inconvenience.";
    }
    $subject = preg_replace('/\s+/', ' ', $subject);
    # Make sure the subject isn't blank afterwards!
    if (preg_match('/^\s*$/', $subject)) {
    $messages[] = "Please specify a subject for your message.";
    }

    $body = $_POST['body'];
    # Make sure the message has a body
    if (preg_match('/^\s*$/', $body)) {
    $messages[] = "Your message was blank. Did you mean to say " .
    "something?"; 
    }
      if (count($messages)) {
        foreach ($messages as $message) {
          echo("<p>$message</p>\n");
        }
        echo("<p>Click the back button and correct the problems. " .
          "Then click Send Your Message again.</p>");
      } else {
    mail($recipient,
          $subject,
          $body,
          "From: $realName <$email>\r\n" .
          "Reply-To: $realName <$email>\r\n"); 
        echo("<p>Your message has been sent. Thank you!</p>\n");
      }
    ?>
    </body>
    </html>