2

I have a form containing tabular data, with each row having a checkbox with the same name so that it gets passed via POST as an array to a PHP page. Everything work fine, but I have an issue relating to when none of the items on the page are selected - this is a special case that I need to handle in a specific way, but I am trying to figure out how to determine the best way to tell when this condition occurs, as when it does the $_POST array is completely empty.

Any strategies to help in determining when an empty set of data has been POSTed to a page in PHP?

Unpossible
  • 10,607
  • 22
  • 75
  • 113

9 Answers9

8

Use the empty function

if( empty($_POST) ) {
//do empty $_POST stuff here
}
Drizzt321
  • 993
  • 13
  • 27
  • Right... but wouldn't that also return true if the page was not posted to, i.e somebody just accessed the page directly via a link? – Unpossible Sep 14 '11 at 23:13
  • Correct, but as Paul Grime commented above, that's how you'd find out the HTTP method. However, the page still could have been POSTed, just someone might not have sent in any form data. – Drizzt321 Sep 14 '11 at 23:23
  • Yes, I am trying to determine that exactly, when someone has POSTed without any form data. Checking that $_POST is empty AND the request method seems like the right solution. – Unpossible Sep 14 '11 at 23:29
4

You can accomplish this a few different ways.

//Method 1
if($_POST) {
//Do Stuff
}

//Method 2
if(!empty($_POST)) {
//Do Stuff
}

//Method 3 - For detecting if a form was submitted
<input type="submit" name="submit" />
if(sizeof($_POST)>1) {
//Do Stuff
}

Method 2 will fail if your value is 0, for a checkbox you need not worry though.

Method 3 relies on you giving your submit button a name, so it is at least submitted when nothing is checked. Then you can see if sizeof() returns more than 1 to see if anything was checked.

DEMO: http://wecodesign.com/demos/stackoverflow-7424062.php

  • in reference to the isset($_POST), won't it always be set even if there was no POST data? Or does it depend on if it's HTTP POST vs HTTP GET? – Drizzt321 Sep 14 '11 at 23:11
  • @Ben you're correct, I've removed that example. I've added a demo link demonstrating my two methods. –  Sep 14 '11 at 23:33
  • The output in your demo is identical if a) I simply visit the page and b) if I submit the form with nothing clicked. As I mention in the question, I need to determine the special case when the POSTed data is empty AND the user has submitted the form rather than simply visited the page. – Unpossible Sep 14 '11 at 23:36
  • I think you misunderstand. Everything will be false upon going to the page because you've not submitted anything. If you don't submit anything, it remains false; however, if you select some of the checkboxes it changes to true. –  Sep 14 '11 at 23:45
  • I understand that, but again, my page takes action upon everything being false..., false = deletion, so I need to check what kind of false, false that they are there via a click, etc or false because they submitted an empty form. – Unpossible Sep 15 '11 at 02:11
  • What it sounds like you want to do is give your submit button a name so it will at least be in the $_POST and then detect if $_POST is above 1, meaning more than the submit button was posted. I'll update my example above, I've already updated my demo. –  Sep 15 '11 at 05:50
4

Add a hidden input field to the page with a known value. This field will always be passed in with the POST data, therefore you will know that the user landed via form submission rather than direct URL. It's as simple as:-

<input type='hidden' name='posted' value='true'>

Purpletoucan
  • 6,472
  • 2
  • 21
  • 28
  • Ah yes, of course, an extra hidden field. I'm going to use the `$_SERVER['REQUEST_METHOD']` solution I think, but this would work just fine as well, thanks. – Unpossible Sep 14 '11 at 23:27
2

I think you've answered your own question. If the $_POST array is empty then there are no checked checkboxes.

Paul Grime
  • 14,970
  • 4
  • 36
  • 58
  • Right, but how do I tell if the form was submitted then... I need to differentiate between case A, user just visits page, and case B, user has submitted the form back to the page with no data posted. – Unpossible Sep 14 '11 at 23:15
  • 1
    Ah I see, then check this link out to see how to determine if the request was a POST as well - http://stackoverflow.com/questions/1538065/find-out-http-method-in-php. – Paul Grime Sep 14 '11 at 23:18
  • Perfect, saves me passing extra data. – Unpossible Sep 14 '11 at 23:22
2
<form>
<input type="text" name="user" value="" />
<input type="submit" name="post" value="Save" />
</form>

//php
if (isset($_POST['post']))
{
   //code here
}
ZigZag
  • 539
  • 1
  • 8
  • 19
0

This will check if any form values have been entered - assuming default input value = ''

$post = array_filter($_POST,'strlen'); //filter all empty values

//if html input submit button has NO name value       
if (sizeof($post)):
      //Do stuff
    endif;

// OR if html input submit button HAS a name value
    if (sizeof($post) > 1):
      //Do stuff
    endif;

You could use a callback function if exact filtering was required

$post = array_filter($_POST,function ($k){ return $k != '' || $k != 'my default value' || *some other condition etc etc* ; });
hejhog
  • 243
  • 1
  • 2
  • 8
0
if ( !empty( $_POST["field"] ) ) {
    // Field sent
} else {
    // Field empty
}
Will
  • 19,661
  • 7
  • 47
  • 48
0

(count($_POST) == 0) //returns boolean

or do you mean when the form is posted but no information is entered?

klj613
  • 177
  • 1
  • 2
  • 10
  • Right, when the form is posted but with no data - in my form's case, the no data means to delete all of the entries so I need to determine when posted && empty. – Unpossible Sep 14 '11 at 23:16
  • Why not force the user to check the "All" box? Sometimes users might forget to check a box when they meant to. Or at the least, popup a modal dialog asking if they are sure they want to delete everything and force them to say OK. – Drizzt321 Sep 14 '11 at 23:30
  • Because I am using the checkbox to allow the user to set something to enabled/disabled. Unchecking it sets it to disabled. Forcing them to check the box doesn't make sense in this case. – Unpossible Sep 14 '11 at 23:42
0

Post data is available when a form is submitted. Given the following:

if($_POST)
{
    // Bar
}

// Foo

If the form is not submitted Foo will be performed.

If the form is submitted Bar will be performed and then Foo.

Given the following:

if ($_POST)
{
    // Bar
}
else
{
    // Foo
}

If the form is not submitted Foo will be performed.

If the form is submitted Bar will be performed.

As for your other question, checking for empty or appropriate data is basic server-side form validation. If you use a library that can be as simple as:

if ($_POST)
{
    $form_helper = new FormHelper();

    $form_helper->validate($_POST["email"], "email");
    $form_helper->validate($_POST["password"], "password");

    if (! $form_helper->notifications())
    {
        // Bar
    }
}

For your specific case (and without a library) it might be:

if ($_POST)
{
    if (empty($_POST["checklist"])
    {
        // Delete all entries.
    }
    else
    {
        // Do something else.
    }

    // Foo
}
Fake Code Monkey Rashid
  • 13,731
  • 6
  • 37
  • 41
  • Right, but as I mentioned, I need to handle the case where the data is submitted via POST AND empty, wouldn't these examples also fire off the code Foo when a user simply visits the form page? – Unpossible Sep 14 '11 at 23:24
  • @Paul: It won't fire unless there is post data. As for empty, I just showed you. You need to validate the form data. – Fake Code Monkey Rashid Sep 14 '11 at 23:28
  • I think there is a misunderstanding... please explain how the code in Foo does NOT get executed when a user is simply visiting the page without POSTing data. – Unpossible Sep 14 '11 at 23:31
  • @Paul: Move it inside the $_POST code block then? I read some other comments and changed my example to what I think you are trying to do. – Fake Code Monkey Rashid Sep 14 '11 at 23:40
  • That won't work. $_POST will be empty with nothing on the form selected, but I still need to act on the fact that the form was submitted empty - that is a valid user interaction for this form (i.e. a user has actively unchecked all the table entries and hit submit, with the end result being all unchecked entries are removed. – Unpossible Sep 14 '11 at 23:45
  • @Paul: Even if the form is empty the submit itself is still part of the post unless you removed it by hand somehow. – Fake Code Monkey Rashid Sep 14 '11 at 23:52
  • check this demo linked above, there is nothing sent to $_POST on empty submit - http://wecodesign.com/demos/stackoverflow-7424062.php – Unpossible Sep 14 '11 at 23:53
  • 1
    @Paul: That's right because you forgot the name attribute. Add it and see what happens. – Fake Code Monkey Rashid Sep 14 '11 at 23:57