Почему я получаю «Неожиданный $ end» в моем php-коде?

#php

#php

Вопрос:

Я не знаю, что я делаю не так. Я просматривал другие форумы, в которых говорится, что моя проблема может быть связана с отсутствием закрытых фигурных скобок или короткого тега php <? ... . Насколько я могу судить, у меня нет ничего из этого. Это форма, которая позволяет вам узнать, остались ли какие-либо поля пустыми.

 <?php

if (count($_POST) > 0)
{

    function check_if_field_submitted($field_to_check)
    {
        if (isset($_POST[$field_to_check]) amp;amp; $_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) amp;amp; 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']) amp;amp; $_POST['hobbies'] == 'movies') { echo "checked='checked'"; } ?> /> Movies
                  <input type="checkbox" name="hobbies" value="sports" <?php if(isset($_POST['hobbies']) amp;amp; $_POST['hobbies'] == 'sports') { echo "checked='checked'"; } ?> /> Sports
                  <input type="checkbox" name="hobbies" value="books" <?php if(isset($_POST['hobbies']) amp;amp; $_POST['hobbies'] == 'books') { echo "checked='checked'"; } ?> /> Books
                  <input type="checkbox" name="hobbies" value="vgames" <?php if(isset($_POST['hobbies']) amp;amp; $_POST['hobbies'] == 'vgames') { echo "checked='checked'"; } ?> /> Video Games 
                  <input type="checkbox" name="hobbies" value="science" <?php if(isset($_POST['hobbies']) amp;amp; $_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']) amp;amp; $_POST['year'] == '1') { echo "checked='checked'"; } ?> /> Freshman
                <input type="radio" name="year" value="2" <?php if(isset($_POST['year']) amp;amp; $_POST['year'] == '2') { echo "checked='checked'"; } ?> /> Sophomore
                <input type="radio" name="year" value="3" <?php if(isset($_POST['year']) amp;amp; $_POST['year'] == '3') { echo "checked='checked'"; } ?> /> Junior
                <input type="radio" name="year" value="4" <?php if(isset($_POST['year']) amp;amp; $_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']) amp;amp; $_POST['university'] == '') { echo "selected='selected'"; } ?> >Please Select an Option</option>
                    <option value="1" <?php if(isset($_POST['university']) amp;amp; $_POST['university'] == '1') { echo "selected='selected'"; } ?> >Florida State University</option>
                    <option value="2" <?php if(isset($_POST['university']) amp;amp; $_POST['university'] == '2') { echo "selected='selected'"; } ?> >University of Florida</option>
                    <option value="3" <?php if(isset($_POST['university']) amp;amp; $_POST['university'] == '3') { echo "selected='selected'"; } ?> >University of Central Florida</option>
                    <option value="4" <?php if(isset($_POST['university']) amp;amp; $_POST['university'] == '4') { echo "selected='selected'"; } ?> >University of Miami</option>
                </select>

            </fieldset>

            <fieldset>
                <button type="submit">Submit</button>
           </fieldset>

        </form>
    </body>
</html>
  

Комментарии:

1. Во-первых, вы используете в два раза больше тега html, как вы можете видеть: <DOCTYPE html> <html> Кроме того, вы определяете функцию внутри первого, если это не так хорошо, чтобы писать понятный код.

2. Я считаю 11 «{» и только 10 «}», так что да, один фигурный { не закрыт…

3. Вы должны проверить Notepad , в нем есть такие функции, как выделение соответствующих скобок и тому подобное.

Ответ №1:

Проблема:

  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 с сопоставлением / выделением скобок.

Ответ №2:

Похоже, вам не хватает закрывающей фигурной скобки в одном из ваших операторов 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');
        }