0

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);
}

?>

7 Answers 7

4

loop through the $_POST array

$error_msg = '';
foreach($_POST as $key => $val){
    if(empty($val)){ 
        $error = 'true'; 
        $error_msg .= $key." missing.\n"; 
    }
}     
Sign up to request clarification or add additional context in comments.

3 Comments

Nice one - will combine with andrewsi's adjustments!
note that you may want to add a layer of validation to remove any potential $_POST fields that you don't want... just add them to the if() statement (for example if your Submit button is blank for value or you want to allow optional fields)
an example would be if($key!='Submit' && empty($val)){ which removes the Submit field from being evaluated
2

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/

Comments

2

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.

2 Comments

Thanks, in terms of the if's on the POST data, would I still have to have seperate ones for each form item, that is the major pain in the butt!
You'll need one per entry, yes. If it's something you're doing a lot of, I'd recommend looking into some kind of automation - I ended up writing my own validation code that just took a config file. I'm sure there's plenty of available code out there that would work, too - jmotes suggests one.
0

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){

Comments

0

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

Comments

0

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);

Comments

0
   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;
    }    
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.