2016-06-28 20 views
-1

私は、マテリアライズといくつかのPHPのCSSを使用して「お問い合わせ」フォームを作成しています。ここに私がこれまで持っていたコードがあります。シンプルなPHP「お問い合わせ」フォーム

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 = '[email protected]'; 
     $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上でこれを実行すると、私は私のエラーメッセージのすべてをバック受け取ります!私は何が間違っているのかわからないし、私は私の髪を引っ張っている!

+1

この '$ _POST ['body']'を見て、あなたのフォームにないものを見てください。だから、自分自身に好意を持ち、デバッグする方法を学んでください。 –

+0

あなたのページを読み込むときに、 '$ _POST'は埋められません? –

答えて

0

まず、$ realFirstNameと$ realSecondNameを割り当てていますが、$ realNameという変数で正規表現を使用していますが、これはコードのどこにも表示されません。

$ _POST ['body']はフォームのどこにもないので、$ bodyをチェックする際に同じ問題があります。

あなたのスクリプトは、同じ理由で未定義の可変エラーをたくさん示しています。

関連する問題