ashleek007 Posted May 23, 2007 Share Posted May 23, 2007 Hi All, Just working on some form validation and have hit a problem... at the minute each error message displays individually of each other... but I want them to all display at the same time if the statements are true... for example if someone hasnt put in their Title or Forename or Surname it should show all three error messages... heres the code below... if ( $USER_VARS['TITLE'] == "" ) { $USER_VARS['TITLE_ERR'] = "* Please select your Title from the list"; return 0; } if ( strlen($USER_VARS['FORE']) > 0 ) { if (!preg_match("/[a-z]+/", $USER_VARS['FORE'])) { $USER_VARS['FORE_ERR'] = "* Please ensure First Name contains only letters (A-Z, upper and lower case), spaces, hyphens or apostrophes."; return 0; } } else { $USER_VARS['FORE_ERR'] = "* Please enter a First Name"; return 0; } if ( strlen($USER_VARS['SURN']) < 1 ) { $USER_VARS['SURN_ERR'] = "* Please ensure Surname contains only letters (A-Z, upper and lower case), spaces, hyphens or apostrophes."; return 0; } if ($USER_VARS['DOB_DAY'] == "Day") { $USER_VARS['DOB_ERR'] = "* Please select your Date of Birth"; return 0; } if ($USER_VARS['DOB_MONTH'] == "Month") { $USER_VARS['DOB_ERR'] = "* Please select the month you were born on"; return 0; } if ($USER_VARS['DOB_YEAR'] == "19") { $USER_VARS['DOB_ERR'] = "* Please select the year you were born on"; return 0; } if ($USER_VARS['EMPLOYMENT'] == "Select Employment") { $USER_VARS['EMPLOYMENT_ERR'] = "* Please select your employment status from the list"; return 0; } Thanks for any help you guys can give, Ash Quote Link to comment https://forums.phpfreaks.com/topic/52621-if-statement/ Share on other sites More sharing options...
OOP Posted May 23, 2007 Share Posted May 23, 2007 Hi there, just remove the return 0 from all the if blocks and after all if statments checks if the error array ( in your case $USER_VARS ) is not empty return it to the caller script. regards, OOP Quote Link to comment https://forums.phpfreaks.com/topic/52621-if-statement/#findComment-259642 Share on other sites More sharing options...
Daniel0 Posted May 23, 2007 Share Posted May 23, 2007 Just do like this: $errors[] = "The error message here"; Then when you are done checking for errors, see if count($errors) > 0 and then display the errors in the array. Quote Link to comment https://forums.phpfreaks.com/topic/52621-if-statement/#findComment-259646 Share on other sites More sharing options...
ashleek007 Posted May 23, 2007 Author Share Posted May 23, 2007 cheers all, Daniel0... do you mean like this?? if ($USER_VARS['EMPLOYMENT'] == "") { $USER_VARS['EMPLOYMENT_ERR'] = "* Please select your employment status from the list"; $errors[] = $USER_VARS['EMPLOYMENT_ERR']; } if( count($errors) > 0 ) { echo $errors[]; } Cheers, Ash Quote Link to comment https://forums.phpfreaks.com/topic/52621-if-statement/#findComment-259666 Share on other sites More sharing options...
Daniel0 Posted May 23, 2007 Share Posted May 23, 2007 Yes, except it would be something like this: if(count($errors) > 0) { echo implode('<br />', $errors); } Quote Link to comment https://forums.phpfreaks.com/topic/52621-if-statement/#findComment-259693 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.