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?

0

9 Answers 9

8

Use the empty function

if( empty($_POST) ) { //do empty $_POST stuff here } 
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
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.
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'>

1 Comment

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.
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

6 Comments

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?
@Ben you're correct, I've removed that example. I've added a demo link demonstrating my two methods.
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.
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.
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.
|
2

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

3 Comments

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.
Ah I see, then check this link out to see how to determine if the request was a POST as well - stackoverflow.com/questions/1538065/find-out-http-method-in-php.
Perfect, saves me passing extra data.
2
<form> <input type="text" name="user" value="" /> <input type="submit" name="post" value="Save" /> </form> //php if (isset($_POST['post'])) { //code here } 

Comments

0
if ( !empty( $_POST["field"] ) ) { // Field sent } else { // Field empty } 

Comments

0

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

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

3 Comments

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.
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.
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.
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 } 

8 Comments

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?
@Paul: It won't fire unless there is post data. As for empty, I just showed you. You need to validate the form data.
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.
@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.
@Paul: That's right because you forgot the name attribute. Add it and see what happens.
|
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* ; }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.