2016-06-28 151 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的變量來使用你的正則表達式,這在我的代碼中看不到。

您在檢查$ body時有同樣的問題,因爲$ _POST ['body']不在您窗體中的任何位置。

你的腳本出於同樣的原因給出了很多未定義的變量錯誤。