出现意外的语法错误

时间:2013-02-14 21:16:20

标签: php syntax-error

尝试创建一个表单并从教程站点中获取...它在我看来我有正确数量的括号。应该少吗?无法弄清楚这个错误。

语法错误,第68行意外的'{'../contact.php

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Bunch'; 
    $to = 'me@hotmail.com'; 
    $subject = 'Hi There!';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";


if ($_POST['submit']) {
    if ($name != '' && $email != '') {           
        if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } else {
            echo '<p>You need to fill in all required fields!!</p>';
        }

    }
}
?>

3 个答案:

答案 0 :(得分:9)

你有两个else子句,这是一个语法错误。

if (...) {
  ...
} else if (...) { 
   ...
} else if (...) {
  ...
} else {  <--only ONE allowed
  ...
}

答案 1 :(得分:1)

Marc B's answer钉了它,但我认为你会这样做:

if ($_POST['submit']) {
    if ($name != '' && $email != '') {           
        if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { # from "if (mail ($to, $subject, $body, $from)) {"
            echo '<p>Something went wrong, go back and try again!</p>'; 
        }
    } else { # from "if ($name != '' && $email != '') {"
            echo '<p>You need to fill in all required fields!!</p>';
    }
}

答案 2 :(得分:0)

更改

if ($name != '' && $email != '') {           
        if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } else {
            echo '<p>You need to fill in all required fields!!</p>';
        }

    }

if ($name != '' && $email != '') {           
        if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 

    }else {
            echo '<p>You need to fill in all required fields!!</p>';
        }