Videos
1) Too many if-else statements is a good indication of poor design. You should use a validation class or write your own. A little bit off-topic but this code reminds me the days we had in 1990s :) Why don't you at least use a micro-framework instead of re-inventing the wheel again and again...
2) You can detect if the call is made with ajax or not:
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
...
}
so in the controller you collect the validation errors in an array and if it's a AJAX request convert it to JSON and do ajax validation (check jquery validator) and if not simply handle the errors as you would w/o ajax. I must warn that this obviously won't be as responsive as pure client side validation but, yeah defining all these validation rules over and over is boring. That's why we use frameworks to ease the pain these days...
try this:
if (empty($_POST["fname"]))
{
$nameErr = "Your First Name Is Missing";
}
else
{
$name = $_POST['name'];
if (!preg_match("/^[a-zA-Z a-zA-Z]*$/", $name))
$nameErr = "Your Name Is Missing";
}
if (empty($_POST["age"]))
{
$ageErr = "Your Age Is Missing";
}
else
{
$age = $_POST['age'];
if (!preg_match("/^[0-9]*$/", $age))
$nameErr = "Your Age Is Missing";
}