I’m a total beginner when it comes to php and I am trying to make the error messages for my form when people put in incorrect information i.e. wrong characters or not enough characters in a certain field. I will post my code below and I will post what it needs to say.
Here is the html form
<form method="post" action="assignment_2_add_patron.php">
<p>
First Name: <br>
<input type="text" name="firstname" size="30">
</p>
<p>
Last Name: <br>
<input type="text" name="lastname" size="30">
</p>
<p>
Email: <br>
<input type="text" name="email" size="40">
</p>
<p>
Birth Year: <br>
<input type="text" name="age" size="4">
</p>
<p>
City of Residence: <br>
<select name="city" id="" size="1">
<option value="open">-</option>
<option value="Chicago">Chicago</option>
<option value="Detroit">Detroit</option>
<option value="Toronto">Toronto</option>
</select>
</p>
<p>
<br>
<input type="submit" value="Submit Information">
</p>
[php]<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$city = $_POST['city'];
$age = $_POST['age'];
$current_year = date('Y');
$age = $current_year - $age;
$fullname = "$firstname $lastname";
if (empty($lastname))
{
print "Error: You must enter a Last Name";
print "<br />";
}
if (empty($email))
{
print "Error: You must enter a your email";
print "<br />";
}
if (empty($age))
{
print "Error: You must enter your Birth Year";
print "<br />";
print "</body></html>";
//exit;
}
else
{
if (!is_numeric($age))
{
print "Your Birth Year must be numeric.'".$age."'";
print "<br />";
print "</body></html>";
//exit;
}
}
if (empty($city))
{
print "Error: You must select your city";
print "<br />";
print "</body></html>";
exit;
}
if ($age<16)
{
$age = "Child";
}
if ($age>54)
{
$age = "Senior";
}
if ($age>15 && $age<55)
{
$age = "Adult";
}
print "<p>Name: $fullname </p>";
print "<p>Email: $email </p> ";
print "<p>City: $city </p>";
print "<p>Section: $age </p>";
?>[/php]
It errors fine for the first and last name but I can't figure out how to make it error for the $age part.