dousi8559 2016-06-28 15:59
浏览 43

简单的PHP'联系我们'表格[复制]

I am building a 'Contact Us' form using some CSS from materialize and some PHP. Here is the code I have so far.

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>

My 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>
");
        }
        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>
" .
          "Reply-To: $realName <$email>
"); 
        echo("<p>Your message has been sent. Thank you!</p>
");
      }
    ?>
    </body>
    </html>

When I run this on MAMP I receive ALL of my error messages back! I don't know what I'm doing wrong and I'm pulling my hair out!

</div>
  • 写回答

1条回答 默认 最新

  • doubu1964 2016-06-28 16:04
    关注

    Well, for starters you're assigning $realFirstName and $realSecondName, but you're using your regex with a variable called $realName which i can't see anywhere else in your code.

    You have the same issue when checking $body, as $_POST['body'] isn't anywhere in your form.

    Your script gives a lot of undefined variable errors for the same reason.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)