soycharliente Posted December 14, 2009 Share Posted December 14, 2009 Hola. 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? <?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 ..."; } ?> Link to comment https://forums.phpfreaks.com/topic/185039-form-errors-in-an-array/ Share on other sites More sharing options...
trq Posted December 14, 2009 Share Posted December 14, 2009 Your $errors array will never be empty because even if all fileds are filled out, your are filling the $errors array with values of false. Link to comment https://forums.phpfreaks.com/topic/185039-form-errors-in-an-array/#findComment-976738 Share on other sites More sharing options...
soycharliente Posted December 14, 2009 Author Share Posted December 14, 2009 Return Values Returns FALSE if var has a non-empty and non-zero value. The following things are considered to be empty: "" (an empty string) 0 (0 as an integer) "0" (0 as a string) NULL FALSE array() (an empty array) var $var; (a variable declared, but without a value in a class) So even though FALSE is listed as something that will return true for the function, the value of FALSE in this case doesn't work when it's an array value? Link to comment https://forums.phpfreaks.com/topic/185039-form-errors-in-an-array/#findComment-976740 Share on other sites More sharing options...
rajivgonsalves Posted December 14, 2009 Share Posted December 14, 2009 its not about the empty function your populating your array variable as FALSE so a key will exists only the value will be false, so technically speaking its not empty Link to comment https://forums.phpfreaks.com/topic/185039-form-errors-in-an-array/#findComment-976742 Share on other sites More sharing options...
trq Posted December 14, 2009 Share Posted December 14, 2009 Your not checking any speific array value eg; if (empty($errors['somekey'])) { Your checking the entire $errors array which is itself NOT empty. Link to comment https://forums.phpfreaks.com/topic/185039-form-errors-in-an-array/#findComment-976743 Share on other sites More sharing options...
soycharliente Posted December 14, 2009 Author Share Posted December 14, 2009 You think the best thing to do is change to regular if statements with only array addition inside so it only adds the error string and not the FALSE value? Unless there is something else I can set it to besides FALSE that would change the outcome, but based on your answers it sounds like that's not possible. Link to comment https://forums.phpfreaks.com/topic/185039-form-errors-in-an-array/#findComment-976747 Share on other sites More sharing options...
rajivgonsalves Posted December 14, 2009 Share Posted December 14, 2009 you can filter the array, but I would suggest write normal if statements $errors = array_filter($errors, create_function('$a', 'return ($a===FALSE) ? null : $a;')); Link to comment https://forums.phpfreaks.com/topic/185039-form-errors-in-an-array/#findComment-976750 Share on other sites More sharing options...
soycharliente Posted December 14, 2009 Author Share Posted December 14, 2009 Yeah. I looked at looping and filtering with a splice or something. But this is faster. Thanks. <?php // check fields $errors = array(); if (empty($the_date)) { $errors['the_date'] = '<span class="error">Please choose a date.</span>'; } if (empty($first_name)) { $errors['first_name'] = '<span class="error">Please fill in your first name.</span>'; } if (empty($last_name)) { $errors['last_name'] = '<span class="error">Please fill in your last name.</span>'; } if (empty($email) || !preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*(([,]|[,])\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*$/", $email)) { $errors['email'] = '<span class="error">Please fill in a valid email address.</span>'; } if (empty($phone)) { $errors['phone'] = '<span class="error">Please provide your phone number.</span>'; } if (empty($emergency_contact)) { $errors['emergency_contact'] = '<span class="error">Please provide an emergency contact.</span>'; } if (empty($emergency_phone)) { $errors['emergency_phone'] = '<span class="error">Please provide an emergency phone number.</span>'; } if (empty($major_interest)) { $errors['major_interest'] = '<span class="error">Please pick a major of interest.</span>'; } if (empty($activity1)) { $errors['activity1'] = '<span class="error">Please select a morning activity.</span>'; } if (empty($activity2)) { $errors['activity2'] = '<span class="error">Please select a morning activity.</span>'; } if (empty($activity3)) { $errors['activity3'] = '<span class="error">Please select an afternoon activity.</span>'; } if (empty($activity4)) { $errors['activity4'] = '<span class="error">Please select an afternoon activity.</span>'; } if (empty($interests)) { $errors['interests'] = '<span class="error">Please list your interests.</span>'; } if (empty($accomodations)) { $errors['accomodations'] = '<span class="error">Please answer.</span>'; } if (!(strcmp($acknowledgement, 'I understand') == 0)) { $errors['acknowledgement'] = '<span class="error">Please type the indicated response.</span>'; } ?> Link to comment https://forums.phpfreaks.com/topic/185039-form-errors-in-an-array/#findComment-976755 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.