Merdok Posted March 6, 2010 Share Posted March 6, 2010 I'm setting up some form validation, I need to check all of the required post variables from the form and if they are empty to add them to the $foundErrors variable. So far I have this: if ($_POST['usr_firstname']) { $db_usr_firstname = $_POST['usr_firstname']; } else { $db_usr_firstname = $_POST['usr_firstname']; $foundErrors .= $db_usr_firstname; } This seems a really long winded way of doing it as obviously I need to do this for every required field. Is there a better way? Ideally I'd like something like $mandatory = 'usr_firstname', 'usr_surname', 'usr_username', 'usr_email' ; and then check all the post variables, if its in the mandatory list and is empty then add it to the $foundErrors variable but still retain its own variable name (so the form field will remain populated). Any ideas? Link to comment https://forums.phpfreaks.com/topic/194372-is-there-a-way-to-simplify-this-if-statement/ Share on other sites More sharing options...
Zane Posted March 6, 2010 Share Posted March 6, 2010 Something like this $mandatory[] = "usr_firstname"; $mandatory[] = "usr_surname"; $mandatory[] = "usr_username"; $mandatory[] = "usr_email"; foreach($mandatory as $item) if(!isset($_POST[$item]) || empty($_POST[$item])) $errors[$item] = true; if(count($errors) > 0) echo "You've got errors"; else { // Continue with your script } Link to comment https://forums.phpfreaks.com/topic/194372-is-there-a-way-to-simplify-this-if-statement/#findComment-1022472 Share on other sites More sharing options...
Merdok Posted March 6, 2010 Author Share Posted March 6, 2010 Thats a bingo! Expected it to be harder than that Thank you! Link to comment https://forums.phpfreaks.com/topic/194372-is-there-a-way-to-simplify-this-if-statement/#findComment-1022473 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.