为什么我收到一个&;Unexpected $end&;在我的PHP代码


Why am I getting an "Unexpected $end" in my php code?

我不知道我做错了什么。我一直在看其他论坛,说我的问题可能与没有关闭花括号关闭或短php标签<? ...有关。据我所知,这些我都没有。这是一个让您知道是否有字段为空的表单。

<?php
if (count($_POST) > 0)
{
    function check_if_field_submitted($field_to_check)
    {
        if (isset($_POST[$field_to_check]) && $_POST[$field_to_check] != '')
        {
            return TRUE;
        }
        else
        {
            return "YOU MUST FILL IN THE $field_to_check FIELD!";
        }
    }
    //--------------------------------------------------------
    $error_messages = array();
    //Validate the input
    //Trim the fields
    $_POST['first_name'] = trim($_POST['first_name']);
    $_POST['last_name'] = trim($_POST['last_name']);
    $_POST['comments'] = trim($_POST['comments']);
    $_POST['first_name'] = strip_tags($_POST['first_name']);
    $_POST['last_name'] = strip_tags($_POST['last_name']);
    $_POST['comments'] = strip_tags($_POST['comments']);
    //Required fields:
    if (check_if_field_submitted('first_name') !== TRUE)
    {
        $error_messages[] = check_if_field_submitted('first_name');
    }
    if (check_if_field_submitted('last_name') !== TRUE)
    {
        $error_messages[] = check_if_field_submitted('last_name');    
    }
    if (check_if_field_submitted('hobbies') !== TRUE)
    {
        $error_messages[] = check_if_field_submitted('hobbies');
    }
    if (check_if_field_submitted('university') !== TRUE)
    {
        $error_messages[] = check_if_field_submitted('university');
    }
    if (check_if_field_submitted('year') !== TRUE)
    {
        $error_messages[] = check_if_field_submitted('year');
    }
    if (check_if_field_submitted('comments') !== TRUE)
    {
        $error_messages[] = check_if_field_submitted('comments');
    if (count($error_messages) < 1)
    {
        header("Location: success.php");
    }
}

?>

<DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        if (isset($error_messages) && count($error_messages) > 0)
        {
            echo "<ul>";
            foreach($error_messages as $message)
            {
                echo "<li>$message</li>";
            }
            echo "</ul>";
        }        
        ?>
      <h1>Register or Fail</h1>
         <form method="post" action="index.php">
           <fieldset>
               <label>First Name</label>
               <input type='text' name='first_name' value="<?php if(isset($_POST['first_name'])) { echo $_POST['first_name']; } ;?>" />
                <label>Last Name</label>
                <input type='text' name='last_name' value='<?php if(isset($_POST['last_name'])) { echo $_POST['last_name']; } ;?>'/>
            </fieldset>
           <fieldset>               
                <label>What are your hobbies?</label>
                  <input type="checkbox" name="hobbies" value="movies" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'movies') { echo "checked='checked'"; } ?> /> Movies
                  <input type="checkbox" name="hobbies" value="sports" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'sports') { echo "checked='checked'"; } ?> /> Sports
                  <input type="checkbox" name="hobbies" value="books" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'books') { echo "checked='checked'"; } ?> /> Books
                  <input type="checkbox" name="hobbies" value="vgames" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'vgames') { echo "checked='checked'"; } ?> /> Video Games 
                  <input type="checkbox" name="hobbies" value="science" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'science') { echo "checked='checked'"; } ?> /> FOR SCIENCE!
           </fieldset>
           <fieldset>
                <label>What year are you?</label>
                <input type="radio" name="year" value="1" <?php if(isset($_POST['year']) && $_POST['year'] == '1') { echo "checked='checked'"; } ?> /> Freshman
                <input type="radio" name="year" value="2" <?php if(isset($_POST['year']) && $_POST['year'] == '2') { echo "checked='checked'"; } ?> /> Sophomore
                <input type="radio" name="year" value="3" <?php if(isset($_POST['year']) && $_POST['year'] == '3') { echo "checked='checked'"; } ?> /> Junior
                <input type="radio" name="year" value="4" <?php if(isset($_POST['year']) && $_POST['year'] == '4') { echo "checked='checked'"; } ?> /> Senior
            </fieldset>
           <fieldset>
               <label>What university are you attending?</label>
               <select name="university">
                    <option value="" <?php if(isset($_POST['university']) && $_POST['university'] == '') { echo "selected='selected'"; } ?> >Please Select an Option</option>
                    <option value="1" <?php if(isset($_POST['university']) && $_POST['university'] == '1') { echo "selected='selected'"; } ?> >Florida State University</option>
                    <option value="2" <?php if(isset($_POST['university']) && $_POST['university'] == '2') { echo "selected='selected'"; } ?> >University of Florida</option>
                    <option value="3" <?php if(isset($_POST['university']) && $_POST['university'] == '3') { echo "selected='selected'"; } ?> >University of Central Florida</option>
                    <option value="4" <?php if(isset($_POST['university']) && $_POST['university'] == '4') { echo "selected='selected'"; } ?> >University of Miami</option>
                </select>
            </fieldset>
            <fieldset>
                <button type="submit">Submit</button>
           </fieldset>
        </form>
    </body>
</html>

问题:

 if (check_if_field_submitted('comments') !== TRUE)
        {
            $error_messages[] = check_if_field_submitted('comments');


if (count($error_messages) < 1)
    {
        header("Location: success.php");
    }
}

答:

 if (check_if_field_submitted('comments') !== TRUE)
            {
                $error_messages[] = check_if_field_submitted('comments');
            }

    if (count($error_messages) < 1)
        {
            header("Location: success.php");
        }
    }

如果我是你,我会找一个带有括号匹配/高亮显示的IDE。

看起来你缺少了一个if语句的右括号。

我认为你想改变这些行:

        if (check_if_field_submitted('comments') !== TRUE)
        {
            $error_messages[] = check_if_field_submitted('comments');

:

        if (check_if_field_submitted('comments') !== TRUE)
        {
            $error_messages[] = check_if_field_submitted('comments');
        }