...

View Full Version : checking form fields have been filled in



ShaunM
09-12-2011, 08:33 PM
hello all :)

I know how to check one field and a few fields, an make sure there filled in.


if ( $var1 == "" || $var2 == "" ) { send back an try again stuff }

However I'm designing a 6 step form that has 69 fields in the first step, so using above way is going get long winded fast.

Usually I would do something like below;



$chiBran = $_POST['childBranch'];
$chiMainid = $_POST['childID'];
$buttype = $_POST['confirm'];
if ( $chiBran == "" || $chiMainid == "" ) { send back an try again stuff }


I've never had the need to do this to such a large scale before.

I am using below coding instead of manually setting each var.



foreach ($_POST as $field => $value)
{
if ( $ffirstName == "" )
{
unset($_GET['do']);
$mess = "Required information is missing, please try again.";
include("register-step1.php");
exit();
}
}


The coding works as I wish it to, if the field related to $ffirstname is empty, the form not process. I'm just thinking there is got to be something so simple to add to this that allow me to make sure all fields have a actual value and are not blank.

any help would be greatly appreciated.

do have a feeling that when I see a solution I'm going have a homer simpson DOH! moment but head is done in right now designing this lol

BluePanther
09-12-2011, 08:49 PM
You're sort of on the right track with the foreach, but not quite. I dont know where the $ffirstname var came from however :S

Post information is parsed into the $_POST variable, which forms an array. So, your foreach($_POST as $key => $value) will give $key as the input field name, and $value as it's value. If all $values need to have something in them, just do this:


$errors = array();
foreach($_POST as $key => $value){
if($value == ''){
$errors[$key] = 'was empty.';
}
}

// If there are errors
if(count($errors) > 0){
foreach($errors as $field => $error){
echo $field.' '.$error."\n";
}
// do error handling stuff here, like redirect or die()
}

This will collect errors, display them all, and then do what you tell it to do if there was any validation errors.

ShaunM
09-12-2011, 10:06 PM
$ffirstName is just the name of a field within the form.

I did try the below coding but it never worked.



foreach ($_POST as $field => $value)
{
if ( $value == "" )
{
unset($_GET['do']);
$mess = "Required information is missing, please try again.";
include("register-step1.php");
exit();
}
}


I could use your code but i presume the echo line can be taken out? as dun wish to echo results, just detect an empty field and send them back to page with the form.

BluePanther
09-12-2011, 10:29 PM
I put the echo field in to aid usability. You say you have 69 fields that are required? What if a user was to not fill in, say, 40 of them and didn't know they were required. They would maybe have to submit the form another 39 times before they know? Although, if you had adequate signage on your form itself, labelling fields as required, my method isn't necessary.

My point about $ffirstname is there would of had to have been an assignment like $ffirstname = $_POST['firstname'] which isn't required just yet, making it the logic a wee bit backwards - instead of validating information, then setting variables you were setting variables, then validating (if you get what I mean). Doesn't matter, your new foreach that you posted in post #3 will do your required task :) Although, it might be better to use a header to redirect, instead of just include - but your choice.

ShaunM
09-12-2011, 10:40 PM
your coding works perfectly from what I've tested with it so far.... thanks

not overly fussed about the echo, along as it works... which it does.

my post #3 wouldn't work for some reason, kept saying empty field when all fields where filled in.

usually I would use header to transfer but I also want the form to retain what has been inputted in the form. tend to find unless i send info along with header, visitor would be met with empty form and have to start again cause they missed one field.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum