Validating my form

I have the following code working, in that it inserts into a database if there are values entered in the input boxes and if not, nothing is entered. However when I press submit to test when there is nothing in the boxes the error message isn’t returned. From looking at the code below I was wondering if someone might know why the error message is not being printed out. Is there something I’m missing

input.php

<?php


echo '<form method = "post" action ="confirm.php">';


?>

<table>
<tr>
<td>name 1</td>
    <td><input name="name1" type="text" value="<?php if(isset($_POST) && isset($_POST['name1'])){
		echo $row['name1'];
	}
	?>"/></td>
    <td><span> <?php if(isset($errors)&& isset($errors['name1'])){
		echo $errors['name1'];
	}
	?>
    </span></td>
</tr>
<tr>
<td>name 2</td>
<td><input name="name2" type="text" value="<?php if(isset($_POST)&& isset($_POST['name2'])){
		echo $_POST['name2'];
	}
	?>" /></td>
    <td><span> <?php if(isset($errors)&& isset($errors['name2'])){
		echo $errors['name2'];
	}
	?>
    </span></td>
  </tr>
<tr>
<td>&nbsp;</td><td><input type = "submit" name = "submit" value = "Sent"></td><td></td>
</tr>
</table>
</form>

When I click the submit button when the input boxes are empty it directs me to confirm.php but no error messages show. How can I get the error message working. Any help would be great.

confirm.php

$valid = true;

$errors = array();

if(empty($_POST['name1'])){
	$valid = false;
	$errors['name1'] = 'enter name1';
}
elseif(empty($_POST['name2'])){
	$valid = false;
	$errors['name2'] = 'enter name 2';
}

	
	if($valid == true)
	{
	echo 'Validation complete';

I had a similar issue while trying to use arrays and found using variables much easier. Have a look at this tutorial :