Jump to content

looping through a form to check if any fields are left empy


svgmx5

Recommended Posts

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 ;	
}
}
?>

 

Link to comment
Share on other sites

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>' ;
}
}

 

Link to comment
Share on other sites

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 */) {

    }
    // ...
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.";
   }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.