Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    checking form fields have been filled in

    hello all

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

    PHP Code:
    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;

    PHP Code:
    $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.

    PHP Code:
       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

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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:
    PHP Code:
    $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.

  • Users who have thanked BluePanther for this post:

    ShaunM (09-12-2011)

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    $ffirstName is just the name of a field within the form.

    I did try the below coding but it never worked.

    PHP Code:
     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.

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •