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 13 of 13
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    205
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Check for empty $_POST

    I want to check for an empty $_POST array upon form submission; how can I do this? I've tried using the following code:

    PHP Code:
    if (empty($_POST)) return false;
    print_r($_POST); 
    However, in spite of the $_POST empty the code reaches the print_r line, displaying:

    Array ( [f_name] => [l_name] => [email] => [confirm_email] => [telephone] => )
    Anyone know what do to?

  • #2
    New Coder
    Join Date
    Dec 2009
    Location
    England
    Posts
    84
    Thanks
    1
    Thanked 11 Times in 11 Posts
    PHP Code:
    $i 0;
    foreach (
    $_POST as $value)
    {
        if (empty(
    $value))
        {
            
    $i++;
        }
    }
    # AMT = the amount of filled posts. for example, in your example 
    # $amt would equal 0, if nothing was posted, $amt would also be 0
    $amt count($_POST) - $i

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by AIOBB-Richard View Post
    PHP Code:
    $i 0;
    foreach (
    $_POST as $value)
    {
        if (empty(
    $value))
        {
            
    $i++;
        }
    }
    # AMT = the amount of filled posts. for example, in your example 
    # $amt would equal 0, if nothing was posted, $amt would also be 0
    $amt count($_POST) - $i
    That last line is moot. An empty POST array won't increment $i, hence it will be 0.
    Last edited by MattF; 12-12-2009 at 05:39 AM.

  • #4
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    If the post array had empty values, but had keys, it would return a number. You'd want to check what integer $i is, not $_POST.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Zangeel View Post
    If the post array had empty values, but had keys, it would return a number. You'd want to check what integer $i is, not $_POST.
    Remind me not to post when tired. I've lost the plot.

  • #6
    New Coder
    Join Date
    Dec 2009
    Location
    England
    Posts
    84
    Thanks
    1
    Thanked 11 Times in 11 Posts
    Quote Originally Posted by MattF View Post
    That last line is moot. An empty POST array won't increment $i, hence it will be 0.
    That would be the intended result, yes :P

    Quote Originally Posted by MattF View Post
    Remind me not to post when tired. I've lost the plot.
    Lol

    Quote Originally Posted by Zangeel View Post
    If the post array had empty values, but had keys, it would return a number. You'd want to check what integer $i is, not $_POST.
    Exactly. $i would return 0 if the form has not been submitted, or the form has been submitted with empty data.

    If you are expecting 4 returns, and $i only contains 3, you have a problem,the user did not enter the form correctly.

  • #7
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Taking Richard's code

    PHP Code:
    $i 0;
    foreach (
    $_POST as $value)
    {
        if (empty(
    $value))
        {
            
    $i++;
        }
    }
    if(
    $i 0)
    {
       return 
    false;

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #8
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    I recently posted a variation of this in response to a separate but similar question:
    PHP Code:
    if(isset($_POST['submit'])){
        
    $blank_inputs=0;
        
    $test_array=array_count_values($_POST); //creates a test array

    //the keys of the test array are set as the individual values of the original array
    //the values of the test array are set as the count of occurences of each value in the original array

    //so, for instance, if our posted values looked like this:
    //        $_POST:    ("cat", "dog", "rabbit", "cat", "moose")
    //then array_count_values($_POST) would create an array like this:
    //        $test_array:    ("cat"=>2, "dog"=>1, "rabbit"=>1, "moose"=>1)
    //then, when you check $test_array["cats"] you would return the number 2
    //using this same idea, we can check for blank values...

        
    $blank_inputs=$test_array[""]; //checking the test array to for the count of occurences of "" within the $_POST array...
        
    if($blank_inputs>0){ //display an error message and put the form back up for editing
            
    print "Blank inputs: ".$blank_inputs."<br />\nThis was a failure!<br />\n";
        } 
        else { 
    //    ...do whatever you need to do with a successful submission...
            
    print "No blank input fields - this was a success!";
        }

    In its simplest form the check would just look like this:
    PHP Code:
    $test_array=array_count_values($_POST);
    $blank_inputs=$test_array[""]; 
    Then you can script based on the value of $blank_inputs.

    I'm a relative PHP novice though. There might be better ways. For example, I don't know if this code is any faster than what has been posted so far. It may or may not be optimal but for my brain it's a bit easier to remember and use.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #9
    New Coder
    Join Date
    Mar 2009
    Posts
    52
    Thanks
    4
    Thanked 6 Times in 6 Posts
    Why are you checking for a blank input? I'm wondering if there's a better strategy to solve whatever it is you are doing...

  • #10
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by cmancone View Post
    Why are you checking for a blank input? I'm wondering if there's a better strategy to solve whatever it is you are doing...
    Presumably to avoid processing an incomplete form, but I am only making an educated guess at this.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #11
    New Coder
    Join Date
    Mar 2009
    Posts
    52
    Thanks
    4
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Rowsdower! View Post
    Presumably to avoid processing an incomplete form, but I am only making an educated guess at this.
    Presumably yes, but in that case I'm not sure why this is necessary. Seems better to check required fields individually (then you can let them know which one is missing). Also I usually stick a:

    Code:
    <input type='hidden' name='verified' value='true'>
    somewhere in my code so that I know that my form actually has been submitted.

    In my ~8 odd years of php programming I've never had to do this, so I'm wondering if it's not necessary or if I've just been doing things wrong

  • #12
    New Coder
    Join Date
    Dec 2009
    Location
    England
    Posts
    84
    Thanks
    1
    Thanked 11 Times in 11 Posts
    Quote Originally Posted by cmancone View Post
    Presumably yes, but in that case I'm not sure why this is necessary. Seems better to check required fields individually (then you can let them know which one is missing). Also I usually stick a:

    Code:
    <input type='hidden' name='verified' value='true'>
    somewhere in my code so that I know that my form actually has been submitted.

    In my ~8 odd years of php programming I've never had to do this, so I'm wondering if it's not necessary or if I've just been doing things wrong
    Both ways are fine, you can easily modify the loop to store which values are missing, and pass that back to the form.

  • #13
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by cmancone View Post
    Presumably yes, but in that case I'm not sure why this is necessary. Seems better to check required fields individually (then you can let them know which one is missing). Also I usually stick a:

    Code:
    <input type='hidden' name='verified' value='true'>
    somewhere in my code so that I know that my form actually has been submitted.

    In my ~8 odd years of php programming I've never had to do this, so I'm wondering if it's not necessary or if I've just been doing things wrong
    I suppose the merit of this approach depends to some extent on whether or not any of your input fields are optional and how you plan to notify the user of the missing data. If EVERYTHING is required then you could just load up the form again pre-filled with existing values but showing a generic "must complete all fields" error message to the user.

    Anyway, if nothing else you could still use this check to see if looping through required values is even necessary. If everything is filled in you can skip a lot of switch cases or if/else statements. That might save some processing. I'm not really sure though. I'm garbage at code optimisation.

    Also, if blank values exist you should be able to create an array of their keys like so (untested code found here):
    PHP Code:
    function array_search_values$m_needle$a_haystack$b_strict false){
        return 
    array_intersect_key$a_haystackarray_fliparray_keys$a_haystack$m_needle$b_strict)));

    Example:
    PHP Code:
    <?php
    $array1 
    = array( 'pre'=>'2'123'1''2''3''post'=>2);
    print_rarray_search_values'2'$array1));
    print_rarray_search_values'2'$array1true));
    print_rarray_search_values2$array1true));
    ?>

    /*Will return:
    array(4) {
        ["pre"] => string(1) "2"
        [1] => int(2)
        [4] => string(1) "2"
        ["post"] => int(2)
    }
    array(2) {
        ["pre"] => string(1) "2"
        [4] => string(1) "2"
    }
    array(2) {
        [1] => int(2)
        ["post"] => int(2)
    }*/
    That would help with your error message output. Just run the function array_search_values( "", $_POST); and handle errors based on that instead of any of the above? Dunno, just brain storming here... I always prefer simplicity where possible.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting


  •  

    Posting Permissions

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