I have a form (lets say a registration form) and onsubmit, it calls a function to validate as follows:
$.ajax({
url : "/ajax/validate.php",
type : "POST",
data : $(".form").serialize(),
success : function(data) {
data = $.parseJSON(data);
$.each(data, function(i, item) {
$('#' + i).addClass('errors');
});
}
});
The validate.php looks something like this:
if ($_POST['email'] == '' || filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == false) {
$errors['email'] = 'Not a valid email';
}
if ($errors) {
echo json_encode($errors);
} else {
// insert to db
}
Now all of this code is specifically for the registration form. I'll have many forms on my site. My question is, do I make a new file (like validate.php
) for each individual form? Such as /ajax/save_profile_data.php
etc? Do I keep each form validation separate or is there a clever way to approach this? I don't know if having a validate file for each form on my site is the correct way to do it.