Form errors in an array


Posted on 16th Feb 2014 07:03 pm by admin

I'm processing a form and putting the errors in an array. empty($errors) doesn't seem to do the trick when trying to check to see if the array is empty. Is my understanding of empty()incorrect? Each item is the array is either a string, the error message, or FALSE.

Can anyone help me figure out how to move into my if statement?

Code: <?php
// check fields
$errors = array();
$errors['the_date'] = (empty($the_date)) ? '<span class="error">Please choose a date.</span>' : FALSE;
$errors['first_name'] = (empty($first_name)) ? '<span class="error">Please fill in your first name.</span>' : FALSE;
$errors['last_name'] = (empty($last_name)) ? '<span class="error">Please fill in your last name.</span>' : FALSE;
$errors['email'] = (empty($email) || !preg_match("/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*(([,]|[,])s*w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*)*$/", $email)) ? '<span class="error">Please fill in a valid email address.</span>' : FALSE;
$errors['phone'] = (empty($phone)) ? '<span class="error">Please provide your phone number.</span>' : FALSE;
$errors['emergency_contact'] = (empty($emergency_contact)) ? '<span class="error">Please provide an emergency contact.</span>' : FALSE;
$errors['emergency_phone'] = (empty($emergency_phone)) ? '<span class="error">Please provide an emergency phone number.</span>' : FALSE;
$errors['major_interest'] = (empty($major_interest)) ? '<span class="error">Please pick a major of interest.</span>' : FALSE;
$errors['activity1'] = (empty($activity1)) ? '<span class="error">Please select a morning activity.</span>' : FALSE;
$errors['activity2'] = (empty($activity2)) ? '<span class="error">Please select a morning activity.</span>' : FALSE;
$errors['activity3'] = (empty($activity3)) ? '<span class="error">Please select an afternoon activity.</span>' : FALSE;
$errors['activity4'] = (empty($activity4)) ? '<span class="error">Please select an afternoon activity.</span>' : FALSE;
$errors['interests'] = (empty($interests)) ? '<span class="error">Please list your interests.</span>' : FALSE;
$errors['accomodations'] = (empty($accomodations)) ? '<span class="error">Please answer.</span>' : FALSE;
$errors['acknowledgement'] = (!(strcmp($acknowledgement, 'I understand') == 0)) ? '<span class="error">Please type the indicated response.</span>' : FALSE;

// does the email exist? did they already register?
$errors['checkEmail'] = checkEmail($email);

//print_r($errors);
if (empty($errors))
{
header('Location: thankyou.php');
exit();
//$sql = "INSERT INTO ...";
}
?>

No comments posted yet

Your Answer:

Login to answer
203 Like 42 Dislike
Previous forums Next forums
Other forums

MFC GUI Programming
I am working on a project right now that requires me to create a MFC Windows GUI. Basically it need

generating all possible random letters
hi'

how can i randomize the letters a,b,c,d,e all possible ways, and i want to print the res

Baffled by Undefined Index in Simple Array: Please Help!
Hello. I have a form which posts an array to this script. However, I can't seem to access the values

Clean URLs
Hi I was wandering if anyone has ever implemented URLs on their PHP run website using URLS like wiki

Adding meta tags under Zend FW
Hello there, recently has come to my hands the FTP of a website which is running under Zend, I would

Mail functionality from localhost to server
Hi

I am facing problem of mail functionality.

When i tested mail functionality in my

Query failed issue with php script but works fine in mssql manager!
hi i have the script below which copies data from one table to another but will only insert new data

trim function issues
Hi guys, total noob here...

So I've been tinkering around with a html and am using php to em

Hom to make one url to open together with another url
I have a chat, which i want to be opened, as soon as the users login to the site. As it is now, when

Memory Allocation
Hi folks,
see this ex:

int v=5;
v=v+10;

then what will do the compiler before th

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash