Need help with creating error messages

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.

Which part for $age is not working? Does it output either of the $age errors?

It actually always prints out: Your Birth Year must be numeric. No matter wether the field is empty or has weird characters in it. Or even if I input it correctly. So it always prints out the “else” function.

What if you type cast it?

[php]$age = (int) $_POST[‘age’];[/php]

I actually can’t because it’s for my school assignment and I have to make it error a certain way. I’t has to come back and say that it has to be numeric or that you must enter your birth year. Thank you for looking at this and trying to help, I really appreciate it.

You are never going to get an error on age. Take for example:

[php]echo date(‘Y’) - ‘some string’; // outputs 2013[/php]

If you want to validate $age is an integer you must do it before calculating $current_year - $age;

That information in and of itself has been more useful than anything that my professor has told me so far. I will restructure my code so that it can validate it as an integer.

Thank you!

Sponsor our Newsletter | Privacy Policy | Terms of Service