Form validation not producing the error messages when it should

I recently followed a tutorial on PHP form validation, it worked like a charm. Now I am trying it on my own and it doesn’t seem to be working.

Clicking submit without typing anything should produce an error that fields must be filled out. I only get that error if I click the gender radio buttons and submit.

I am not sure where I went wrong here. Any advice would be very helpful to me. Thank you for your time.

[php] <?php
//determine whether variables have already been declared by PHP, returns a boolean.
if (isset($_POST[‘firstname’], $_POST[‘middleinitial’], $_POST[‘lastname’], $_POST[‘phone1’],
$_POST[‘phone2’], $_POST[‘phone3’], $_POST[‘email’], $_POST[‘address’],
$_POST[‘category’], $_POST[‘gender’])) {
//array to hold error messages
$errors = array();

$firstname = $_POST['firstname'];
$middleinitial = $_POST['middleinitial'];
$lastname = $_POST['lastname'];
$phone1 = $_POST['phone1'];
$phone2 = $_POST['phone2'];
$phone3 = $_POST['phone3'];
$email = $_POST['email'];
$address = $_POST['address'];
$category = $_POST['category'];
$gender = $_POST['gender'];

//make sure fields are not empty, if they are not empty, then break into 
//the else statement which contains the rest of the checks.
if (empty($firstname) || empty($middleinitial) || empty($lastname) || empty($phone1) || empty($phone2) 
|| empty($phone3) || empty($email) || empty($address) || empty($category)|| empty($gender)) {
	//append value to errors array (which we will do again and again)
	$errors[] = "All fields are required.";
} 	else {

		//check age in a number and if it is, check that it's greater than 0.
		if (!is_numeric($phone1) || !is_numeric($phone2) || !is_numeric($phone3)) {
			$errors[] = "Telephone must be a number!";
		} 	else {
			/*strpos — Find position of first occurrence of a string.
			strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
			Searches haystack, for needle, and  will returns the position as an integer. 
			If needle is not found, strpos() will return boolean FALSE, which is how it's used here.
			*/
			if((strpos($phone1,".") !== false) || (strpos($phone2,".") !== false) || (strpos($phone3,".") !== false)){
				$errors[] = 'Age must be a whole number!';
			}
		}
	
		/*Statement below as a whole says " if email validation is false..."
		filter_var(Value to filter, The ID of the filter to apply.) 
		Filters a variable with a specified filter*/
		if (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {
			$errors[] = 'Please enter a valid email address!';
		}
		if (strlen($email) > 50) {
			$errors[] = "Email is too long!";
		}
	}

/*this if method allows to list all errors found from the array, by checking if there
are any errors int he error array, if so, loop through each one of print it.  If there are
no errors, print msg.*/
if (!empty($errors)) {
	foreach ($errors as $error) {
		echo '<strong>', $error, '</strong><br />';
	}	
}	else {
		echo 'You\'ve been registered!';
	}

}
?>
[/php]

HTML [code]

  <table>
  
     <tr>
        <td>Name:  </td>
		<td><input type="text" name="firstname" value="" id="search" placeholder="First name" class="rounded" maxlength="30"/></td>
        
		<td><input type="text" name="middleinitial" value="" id="search" placeholder="Middle Inital" class="rounded" maxlength="1"/></td>
        
		<td><input type="text" name="lastname" value="" id="search" placeholder="Last name" class="rounded" maxlength="30"/></td>
     </tr>
     <tr>
        <td>Telephone:  </td>
		<td><input type="text" name="phone1" value="" id="search" placeholder="Telephone" class="rounded" size="3" maxlength="3"/>-</td>
		<td><input type="text" name="phone2" value="" id="search" placeholder="Telephone" class="rounded" size="3" maxlength="3"/>-</td>
		<td><input type="text" name="phone3" value="" id="search" placeholder="Telephone" class="rounded"  size="4" maxlength="4"/></td>
     </tr>
     <tr>
        <td>Email:  </td>
		<td><input type="text" name="email" value="" id="search" placeholder="Email" class="rounded"  size="20" maxlength="50"/></td>
     </tr>
     <tr>
        <td>Address:  </td>
		<td><input type="text" name="address" value="" id="search" placeholder="Address" class="rounded" /></td>
     </tr>		 
	 <tr>
		<td>Category</td>
		<td align="left">
			
				<select class="rounded"  name="category">
					<option value="livemodel">Live Model</option>
					<option value="museum">Museum</option>
					<option value="subwaysketch">Subway Sketch</option>
				</select>
			
		</td>
     </tr>
     <tr>
        <td>Gender:  </td>
		<td><input id="radio" class="rounded" type="radio" name="gender" value="male" /> Male</td>
		<td><input id="radio" class="rounded" type="radio" name="gender" value="female" /> Female</td>
     </tr>
	</table>

	<p><input type="submit" value="Register" /></p>
[/code]
Sponsor our Newsletter | Privacy Policy | Terms of Service