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 <? ...
. 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_submit开发者_如何转开发ted($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.
It looks like you're missing a closing brace on one of your if statements.
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');
}
精彩评论