简体   繁体   English

为什么我的PHP代码中出现“意外的$ end”?

[英]Why am I getting an “Unexpected $end” in my php code?

I do not know what im doing wrong. 我不知道我做错了什么。 I have been looking through other forums that say my problem might be related to not having closed curly braces closed or a short php tag <? ... 我一直在寻找其他论坛,说我的问题可能与没有关闭大括号关闭或短的PHP标签<? ... <? ... . <? ... I have none of these as far as I can tell. 据我所知,我没有这些。 This is a form that lets you know if any fields are left blank. 这是一个表单,可以让您知道是否有任何字段留空。

<?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>

Problem: 问题:

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




if (count($error_messages) < 1)
    {

        header("Location: success.php");
    }

}

Answer: 回答:

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

            }


    if (count($error_messages) < 1)
        {

            header("Location: success.php");
        }

    }

If I were you I would find an IDE with bracket matching/highlighting. 如果我是你,我会找到一个带支架匹配/突出显示的IDE。

It looks like you're missing a closing brace on one of your if statements. 看起来你错过了一个if语句的结束括号。

I think you want to change these lines: 我想你想改变这些方面:

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

to this: 对此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM