PHP & form validation
        Posted  
        
            by 
                user887515
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user887515
        
        
        
        Published on 2012-09-13T15:29:51Z
        Indexed on 
            2012/09/13
            15:38 UTC
        
        
        Read the original article
        Hit count: 229
        
php
|validation
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);
}
?>
© Stack Overflow or respective owner