2015-07-19 147 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?";