Which field do you need to validate? Lets assume that it’s the first name… First, I would put the form itself into a function so that you can reuse it if the validation fails. Just pass the form function an array that represents the $_POST array. Then echo the appropriate array key in the form field value attribute like so:
echo "<input type=\"text\" name=\"txtFirstName\" value=\"$postValues['txtFirstName']\" />";
Then, after submission:
if(isset($_POST['submit']))
{
//you could use regular expression matching for phone, email
if($_POST['txtFirstName'] != '')
{
$firstName = $_POST['txtFirstName'];
$lastName = $_POST['txtLastName'];
//etc...
}
else
{
echo "<h2>ERROR MESSAGE</h2>";
$form = formFunction($_POST);
}
}
else
{
echo "<h2>FILL OUT FORM</h2>";
$form = formFunction();
}
But I still like to use Javascript validation because it validates before the form is submitted to the server, so it is a little bit quicker