0

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

语法错误,第 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>';
        }

    }
}
?>
4

3 回答 3

9

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

if (...) {
  ...
} else if (...) { 
   ...
} else if (...) {
  ...
} else {  <--only ONE allowed
  ...
}
于 2013-02-14T21:17:44.437 回答
1

Marc B 的回答是肯定的,但我认为你会这样做:

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>';
    }
}
于 2013-02-14T21:20:36.000 回答
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>';
        }
于 2013-02-14T21:27:59.090 回答