2015-07-19 54 views
-1

因此,我正試圖學習如何在XAMPP上構建PHP聯繫表單,只需通過電子郵件發送名稱,電子郵件和消息即可。PHP聯繫表格解析錯誤

所以有一些代碼,我在一對夫婦的教程發現了一些小白混吧「我覺得這個放在這裏」魔術並提出這樣的:那麼

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    </head> 
    <body> 

    <?php 
    //This is where the variables are given empty values 
    $nameErr = $emailErr = $textErr = ""; 
    $name = $email = $message = ""; 

    //The variable for the JavaScript 
    $js = <<<JS 
<script> 
document.getElementById("contactform").style.display = "none"; 
</script> 
JS; 

    if ($_SERVER["REQUEST_METHOD"] == "POST") { 
     if (empty($_POST["name"])) { 
      $nameErr = "Name is required"; 
     } else { 
      $name= test_input($_POST["name"]); 
     } //this checks if the name contains only letters and whitespaces: 
      if (!preg_match("/^[a-zA-Z ]*$/",$name)) { 
       $nameErr = "Only letters and white space allowed"; 
      } 


     if (empty($_POST["email"])) { 
      $emailErr = "Email is required"; 
     } else { 
      $email = test_input($_POST["email"]); 
     } // this is where the e-mail address's form is checked 
      if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
       $emailErr = "Invalid email format"; 
      } 


     if (empty($_POST["message"])) { 
      $textErr = "Aren't you going to say something?" 
     } else { 
      $message = test_input($_POST["message"]); 
     } 

    } 
    function test_input($data) { 
     $data = trim($data); 
     $data = stripslashes($data); 
     $data = htmlentities($data); 
     return $data; 
    } 


    if ($SERVER["REQUEST_METHOD"] == "POST") { 
     if (empty($nameErr && $emailErr && $textErr)) { 
      $to = "[email protected]"; 
      $subject = "Form Submission from "$name"."; 
      $messageall = "$message \n From: $name \n Reply to: $email"; 
      mail($to,$subject,$message,$from); 
      echo $js; 
      echo "Thanks, we will contact you back shortly!" 
     } 
    } 
    ?> 

     <form id="contactform" method="POST" action=""> 
      Name:<br><input type="text" name="yourname"> 
      <?php echo $nameErr;><br> 
      E-mail:<br><input type="text" name="youremail"> 
      <?php echo $emailErr;><br> 
      Message:<br><textarea name="message" rows="6" cols="25"></textarea> 
      <?php echo $textErr;> 
      <br><br> 
      <input type="submit" value="Submit"><input type="reset" value="clear"><br> 
      <?php echo $formErr;> 
     </form> 
    </body> 
</html> 

,當我跑我得到這個頁面:

Parse error: syntax error, unexpected '}' in C:\xampp\htdocs\form\index.php on line 43 

我檢查了我的括號,我不確定我哪裏出錯了。我將它們移動並得到其他解析錯誤。

如果有人會幫助我解決這個問題和其他任何我可能已經搞砸了,但不知道我還沒有...我會非常感激。感謝大家!

+2

你缺少在trainling分號第42行'$ textErr =「你不打算說些什麼嗎?」;' – Yasel

回答

2

我沒有測試它,但我的猜測是,你少在這條線分號:

$textErr = "Aren't you going to say something?" 
+0

謝謝邁克爾,它解決了它(至少那個問題至少是大聲笑)。 –

2

如果你看看第42行,你會注意到你在行尾缺少了一個分號,因此在第43行是意料之外的,因爲它需要一個分號。

更新42行:

$textErr = "Aren't you going to say something?";