svgmx5 Posted June 19, 2010 Share Posted June 19, 2010 I'm have a form, a long form and i want to check to make sure no field was left empty upon form submission. Right now i'm using this but its not working, not sure if i'm on the right track or not, so any help would be gladly appreciate it <?php foreach($_POST as $field){ if(empty($field)){ echo $field ; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/205240-looping-through-a-form-to-check-if-any-fields-are-left-empy/ Share on other sites More sharing options...
kratsg Posted June 19, 2010 Share Posted June 19, 2010 This script literally only outputs empty variables. Try if(!empty()).. instead. Quote Link to comment https://forums.phpfreaks.com/topic/205240-looping-through-a-form-to-check-if-any-fields-are-left-empy/#findComment-1074295 Share on other sites More sharing options...
svgmx5 Posted June 19, 2010 Author Share Posted June 19, 2010 so i added the !empty() instead but it's still not echoing nothing foreach($_POST as $field){ if(!empty($field)){ echo $field ; } } Quote Link to comment https://forums.phpfreaks.com/topic/205240-looping-through-a-form-to-check-if-any-fields-are-left-empy/#findComment-1074297 Share on other sites More sharing options...
svgmx5 Posted June 19, 2010 Author Share Posted June 19, 2010 okay so i got it to partially work...here's what i want it to do to check if each field is empty, if the field is empty then display a message that says' field name required' right now its working to the point that it's looping through the whole form and displaying the error for each field, but i can't get the field name to display here's the code $required_fields = $_POST; foreach($required_fields as $fieldname){ if (empty($_POST[$fieldname])) { echo '<h3 class="required_msg" '.$_POST[$fieldname].' required</h3>' ; } } Quote Link to comment https://forums.phpfreaks.com/topic/205240-looping-through-a-form-to-check-if-any-fields-are-left-empy/#findComment-1074304 Share on other sites More sharing options...
Alex Posted June 19, 2010 Share Posted June 19, 2010 You can't rely on the contents of $_POST in that you can't expect it to contain all of the fields from the form. It's possible someone sent information to the page without those fields. In other words, it would be possible for a required field not to be in the $_POST array, but because you're only looping through it to check if fields are empty the error wouldn't be caught. Instead you should check to see if each field is set and meets requirements explicitly. if(isset($_POST['username'], $_POST['other_field']/*, ...*/)) { // Check to see if all fields are set if(!empty($_POST['username']) /* Other validation required here */) { } // ... } Quote Link to comment https://forums.phpfreaks.com/topic/205240-looping-through-a-form-to-check-if-any-fields-are-left-empy/#findComment-1074305 Share on other sites More sharing options...
mrMarcus Posted June 19, 2010 Share Posted June 19, 2010 okay so i got it to partially work...here's what i want it to do to check if each field is empty, if the field is empty then display a message that says' field name required' right now its working to the point that it's looping through the whole form and displaying the error for each field, but i can't get the field name to display here's the code $required_fields = $_POST; foreach($required_fields as $fieldname){ if (empty($_POST[$fieldname])) { echo '<h3 class="required_msg" '.$_POST[$fieldname].' required</h3>' ; } } Because you're using empty() instead of !empty() again. If something is empty/has no value, how do you expect to display a value? Doesn't make any sense. Quote Link to comment https://forums.phpfreaks.com/topic/205240-looping-through-a-form-to-check-if-any-fields-are-left-empy/#findComment-1074306 Share on other sites More sharing options...
svgmx5 Posted June 19, 2010 Author Share Posted June 19, 2010 alexWD: Thats' usually what i do, however this is long form, and i'm just trying to see if there is a way to automatically check the empty fields of a form. mrMarcus: I used !empty instead, but now i'm not getting any single errors outputed Quote Link to comment https://forums.phpfreaks.com/topic/205240-looping-through-a-form-to-check-if-any-fields-are-left-empy/#findComment-1074310 Share on other sites More sharing options...
theverychap Posted June 19, 2010 Share Posted June 19, 2010 foreach ($_POST as $k=>$v) { if ( !isset($k) && empty($k) ) { // display error } } Quote Link to comment https://forums.phpfreaks.com/topic/205240-looping-through-a-form-to-check-if-any-fields-are-left-empy/#findComment-1074355 Share on other sites More sharing options...
cags Posted June 19, 2010 Share Posted June 19, 2010 foreach ($_POST as $k=>$v) { if ( !isset($k) && empty($k) ) { // display error } } There are quite a few issues with that code. Firstly isset will always return true, because the foreach loop is always going to set it. Secondly you only check empty if it isn't set, which would automatically throw an undefined notice (if it were possible for it to not be set). Also the only instance in which $k could be empty is if it happens to have the array key '0' or somehow got set as null. Neither of which is relevant to the question at hand. AlexWD is on the right track with manually setting up a check for each field, because you can't guarantee that unfilled out form items will appear in the $_POST array. Since empty text fields normally do end up in the $_POST array, the simplest hack you could use is... foreach($_POST as $k=>$v) { if(empty($v)) { echo "$k is a required field."; } } Quote Link to comment https://forums.phpfreaks.com/topic/205240-looping-through-a-form-to-check-if-any-fields-are-left-empy/#findComment-1074383 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.