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

2 つの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 に答える