Simple PHP 'Contact Us' Form


Simple PHP 'Contact Us' Form

我正在使用一些CSS从materialize和一些PHP建立一个'Contact Us'表单。这是我到目前为止的代码。

HTML

<div class="row">
        <form action="?" method="post">
          <div class="row">
            <div class="input-field col s6">
              <input name="realfirstname" id="realfirstname" type="text">
              <label class="active">First Name</label>
            </div>
            <div class="input-field col s6">
              <input name="realsecondname" id="realsecondname" type="text" class="validate">
              <label class="active" for="last_name">Last Name</label>
            </div>
          </div>
          <div class="row">
            <div class="input-field col s12">
              <input name="email" id="email" type="text" class="validate">
              <label class="active" for="last_name">Email</label>
            </div>
          </div>
          <div class="row">
            <div class="input-field col s12">
              <input name="comments" id="comments" type="text" class="materialize-textarea">
              <label class="active" for="textarea1">Comments</label>
            </div>
          </div>
          <div class="row">
            <button class="btn waves-effect waves-light" type="submit" name="send">Submit
            </button>
          </div>
        </form>
      </div>
    </div>
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>

当我在MAMP上运行此操作时,我收到了所有错误消息!我不知道我做错了什么,我快把头发拔出来了!

好吧,对于初学者你分配$realFirstName和$realSecondName,但你使用你的正则表达式与一个变量称为$realName,我不能在你的代码中看到其他任何地方。

当检查$body时,你也会遇到同样的问题,因为$_POST['body']不在你的表单中。

由于同样的原因,你的脚本给出了很多未定义的变量错误。