Reputation: 804
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);
}
?>
Upvotes: 0
Views: 520
Reputation: 5239
if(empty($_POST["emailaddress"]) || empty($_POST["password"]) ||
empty($_POST["firstname"]) || empty($_POST["lastname"]) ){
$error = TRUE;
if ( empty($_POST["emailaddress"]) )
$field = 'Email Address';
else if ( empty($_POST["password"]) )
$field = 'Password';
else if ( empty($_POST["firstname"]) )
$field = 'First Name';
else
$field = 'Last Name';
$validation_msg = $field . ' missing.';
if(empty($error_msg)){
$error_msg .= $validation_msg;
} else{
$error_msg .= '\n' . $validation_msg;
}
}
Upvotes: 0
Reputation: 14856
As your code is pretty repetitive you should think about writing a function that does the validation:
$msgStack = array();
function validate($field, $msg, $msgStack) {
if(empty($field)) {
$error = 'true';
$msgStack[] = $msg;
}
}
and call it like
validate($_POST['firstname'], 'Firstname is empty', $msgStack);
and then output all messages like
echo implode(PHP_EOL, $msgStack);
Upvotes: 0
Reputation: 1001
loop through the $_POST
array
$error_msg = '';
foreach($_POST as $key => $val){
if(empty($val)){
$error = 'true';
$error_msg .= $key." missing.\n";
}
}
Upvotes: 4
Reputation: 18550
How about
<?php
$array = new array{{"firstname","First Name"},{"lastname", "Last Name"}};
Then loop though the array
if(empty($_POST[$array[i][0])){ $error = 'true'; $validation_msg .= $array[i][1] ' missing.' . "\n";
I dont have access to PHP right now so not tested but the idea will work. Code may need tweaking
Upvotes: 0
Reputation: 14681
One big improvement would me to make $error_msg
an array, that will remove the if (empty($error_msg)) {}
part.
$error_msg = array();
Then add error messages using:
$error_msg[] = $validation_msg;
Then you can remove $error = 'true'
every time you find an error, and at the end verify the content of your $error_msg
array:
if(count($error_msg) > 0){
Upvotes: 0
Reputation: 10732
Try something like this:
$error_msg = array()
if(empty($_POST["lastname"])){
$error_msg[] = 'Last name missing.';
}
....
if($error_msg){
header('HTTP/1.1 500 Internal Server Error');
header('Content-Type: application/json');
die(implode("\n", $error_msg);
}
It generates an array of error messages. If there's anything in the array, implode it into a string and return that.
Upvotes: 2
Reputation: 2739
I'd recommend using the php Zebra Form library. It allows you to build your validation rules in a object oriented way and automatically generates javascript to do client-side validation as well.
http://stefangabos.ro/php-libraries/zebra-form/
Upvotes: 2