Jump to content

checking every form field


jimfog

Recommended Posts

I have made a function which loops through a form and if these are left empty then return false.Here is the function:

function filled_out_biz_user($post){	 global $services,$pricelist,$wwwaddress;	  $services=array();  $pricelist=array();  $required=array();   $required['name']=$_POST['name'];  $required['lastname']=$_POST['lastname'];  $required['password']=$_POST['password'];  $required['phone']=$_POST['phone'];  $required['e-mail']=$_POST['e-mail'];  $required['address']=$_POST['address'];  $required['city']=$_POST['city'];  $required['municipality']=$_POST['municipality'];    $services['service1']=$_POST['service1'];   $pricelist['price1']=$_POST['price1'];  $wwwaddress==$_POST['wwwaddress'];   $wasFilledOutCompletely = true;  foreach ($required as $value) {		 if ($value == '') {			    $wasFilledOutCompletely =  false;		 }  }  return $wasFilledOutCompletely;}

Just concentrate on the required fields(for now) The problem with the above logic though is that it does not return false if empty for each individually-which is my goal in this case.I mean if half the fields are non-empty it will say "You have not entered all the fields". What I want to achieve is...:"You have not entered the following fields"...I mean being more specific about it.I do not know if this can be done by modifying more or less the above function or change drastically.

Link to comment
Share on other sites

foreach will traverse the index which is already exists. So it will never return false. You can though maintain an list of array of fields which are required and check against the $_POST or $_GET if it has been set or not. there is also option for required flag in filter_var() http://php.net/filter_var

Link to comment
Share on other sites

Have an array of messages related to each field input, if not filled, take the related message from message array, and do what you want with it.
probably this is the way to go, which means also that I have to throw away filled_out(). Here is an example where first is checked if a form field is empty and if not proceed with filtering operations-more or less, I think this is what you are suggesting:
 		    if(trim($_POST['name'])=='')		    {$empty.= 'you have to enter a name';		    $emptyclass['name']=true;		    }		    elseif (checkname($_POST['name']) == false) {				 $errors.= 'You have to enter a name';				 $errorclass['name'] = true;			 }    if($empty || $errors){echo .....}			

checkname() is just a filtering function where sanitization and validation takes place.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...