php - Checking specific fields in a contact form -


in contact form, want check see if field empty. if is, i'll return error message.

however, want check fields, since not of fields i'm including required.

    $fields = [             'company name' => $_post['companyname'],             'name' => $_post['name'],             'email' => $_post['email'],             'phone' => $_post['phone'],             'comment' => $_post['comment'],         ];  foreach($fields $field => $data) {                 if(empty($data)) {                     $errors[] = 'the ' . $field . ' field required.';                 } 

how go making exceptions fields? thinking of adding required fields class, perhaps there's way i'm not aware of.

use array fields should validated:

$validate = array('name', 'email');  $fields = [     'company name' => $_post['companyname'],     'name' => $_post['name'],     'email' => $_post['email'],     'phone' => $_post['phone'],     'comment' => $_post['comment'], ];  foreach ($fields $field => $data) {     if (in_array($field, $validate) && empty($data)) {         $errors[] = 'the ' . $field . ' field required.';     } } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -