PHP & form validation
- by user887515
I have a form that I'm submitting via ajax, and I want to return a message of the list of fields that were empty.
I've got that all done and dusted, but it just seems really long winded on the PHP side of things.
How can I do the below in a less convoluted way?
<?php
if(empty($_POST["emailaddress"])){
$error = 'true';
$validation_msg = 'Country missing.';
if(empty($error_msg)){
$error_msg .= $validation_msg;
} else{
$error_msg .= '\n' . $validation_msg;
}
}
if(empty($_POST["password"])){
$error = 'true';
$validation_msg = 'Country missing.';
if(empty($error_msg)){
$error_msg .= $validation_msg;
} else{
$error_msg .= '\n' . $validation_msg;
}
}
if(empty($_POST["firstname"])){
$error = 'true';
$validation_msg = 'First name missing.';
if(empty($error_msg)){
$error_msg .= $validation_msg;
} else{
$error_msg .= '\n' . $validation_msg;
}
}
if(empty($_POST["lastname"])){
$error = 'true';
$validation_msg = 'Last name missing.';
if(empty($error_msg)){
$error_msg .= $validation_msg;
} else{
$error_msg .= '\n' . $validation_msg;
}
}
if($error){
header('HTTP/1.1 500 Internal Server Error');
header('Content-Type: application/json');
die($error_msg);
}
?>