Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, September 9, 2016

PHP & form validation

PHP & form 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?

  

Answer by jmotes for PHP & form validation


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/

Answer by andrewsi for PHP & form validation


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.

Answer by Tchoupi for PHP & form validation


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

Answer by exussum for PHP & form validation


How about

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

Answer by Dave for PHP & form validation


loop through the $_POST array

$error_msg = '';  foreach($_POST as $key => $val){      if(empty($val)){           $error = 'true';           $error_msg .= $key." missing.\n";       }  }       

Answer by acme for PHP & form validation


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

Answer by Teena Thomas for PHP & form validation


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


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.