Web Analytics Made Easy -
StatCounter checking form fields have been filled in - CodingForum

Announcement

Collapse
No announcement yet.

checking form fields have been filled in

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

    Comment


    • #3
      $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.

      Comment


      • #4
        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.
        Useful function to retrieve difference in times
        The best PHP resource
        A good PHP FAQ
        PLEASE remember to wrap your code in [PHP] tags.
        PHP Code:
        // Replace this
        if(isset($_POST['submitButton']))
        // With this
        if(!empty($_POST))
        // Then check for values/forms. Some IE versions don't send the submit button 
        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

        Comment


        • #5
          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.

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎