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
Post a Comment