Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-11-2009, 10:10 PM   PM User | #1
Dan06
Regular Coder

 
Join Date: Sep 2008
Posts: 205
Thanks: 1
Thanked 0 Times in 0 Posts
Dan06 is an unknown quantity at this point
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:

Quote:
Array ( [f_name] => [l_name] => [email] => [confirm_email] => [telephone] => )
Anyone know what do to?
Dan06 is offline   Reply With Quote
Old 12-11-2009, 10:18 PM   PM User | #2
AIOBB-Richard
New Coder

 
Join Date: Dec 2009
Location: England
Posts: 84
Thanks: 1
Thanked 11 Times in 11 Posts
AIOBB-Richard is on a distinguished road
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
AIOBB-Richard is offline   Reply With Quote
Old 12-12-2009, 05:37 AM   PM User | #3
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
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..
MattF is offline   Reply With Quote
Old 12-12-2009, 05:57 AM   PM User | #4
Zangeel
Regular Coder

 
Zangeel's Avatar
 
Join Date: Oct 2007
Location: public_html/
Posts: 638
Thanks: 17
Thanked 79 Times in 79 Posts
Zangeel will become famous soon enough
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]
Zangeel is offline   Reply With Quote
Old 12-12-2009, 06:12 AM   PM User | #5
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
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.
MattF is offline   Reply With Quote
Old 12-12-2009, 12:44 PM   PM User | #6
AIOBB-Richard
New Coder

 
Join Date: Dec 2009
Location: England
Posts: 84
Thanks: 1
Thanked 11 Times in 11 Posts
AIOBB-Richard is on a distinguished road
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.
AIOBB-Richard is offline   Reply With Quote
Old 12-12-2009, 07:27 PM   PM User | #7
whizard
Senior Coder

 
whizard's Avatar
 
Join Date: Jan 2005
Location: Philadelphia, PA, USA
Posts: 1,457
Thanks: 10
Thanked 37 Times in 37 Posts
whizard will become famous soon enoughwhizard will become famous soon enough
Taking Richard's code

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

Dan
__________________
If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.
Step 1: Learn. Step 2: Search. Step 3: Post here.
whizard is offline   Reply With Quote
Old 12-12-2009, 10:43 PM   PM User | #8
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,015
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
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
Rowsdower! is offline   Reply With Quote
Old 12-13-2009, 03:19 AM   PM User | #9
cmancone
New Coder

 
Join Date: Mar 2009
Posts: 52
Thanks: 4
Thanked 6 Times in 6 Posts
cmancone is an unknown quantity at this point
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...
cmancone is offline   Reply With Quote
Old 12-13-2009, 03:23 AM   PM User | #10
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,015
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
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
Rowsdower! is offline   Reply With Quote
Old 12-13-2009, 03:48 AM   PM User | #11
cmancone
New Coder

 
Join Date: Mar 2009
Posts: 52
Thanks: 4
Thanked 6 Times in 6 Posts
cmancone is an unknown quantity at this point
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
cmancone is offline   Reply With Quote
Old 12-13-2009, 04:00 PM   PM User | #12
AIOBB-Richard
New Coder

 
Join Date: Dec 2009
Location: England
Posts: 84
Thanks: 1
Thanked 11 Times in 11 Posts
AIOBB-Richard is on a distinguished road
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.
AIOBB-Richard is offline   Reply With Quote
Old 12-14-2009, 03:20 PM   PM User | #13
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,015
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
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
Rowsdower! is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:41 AM.


Advertisement
Log in to turn off these ads.