Unpossible
Unpossible

Reputation: 10697

Determine if empty $_POST submitted

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?

Upvotes: 2

Views: 18176

Answers (9)

hejhog
hejhog

Reputation: 243

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* ; });

Upvotes: 0

user888750
user888750

Reputation:

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

Upvotes: 4

Fake Code Monkey Rashid
Fake Code Monkey Rashid

Reputation: 14557

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
}

Upvotes: 0

ZigZag
ZigZag

Reputation: 549

<form>
<input type="text" name="user" value="" />
<input type="submit" name="post" value="Save" />
</form>

//php
if (isset($_POST['post']))
{
   //code here
}

Upvotes: 2

klj613
klj613

Reputation: 177

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

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

Upvotes: 0

Purpletoucan
Purpletoucan

Reputation: 6572

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

Upvotes: 4

Paul Grime
Paul Grime

Reputation: 15104

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

Upvotes: 2

Will
Will

Reputation: 20235

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

Upvotes: 0

Drizzt321
Drizzt321

Reputation: 1021

Use the empty function

if( empty($_POST) ) {
//do empty $_POST stuff here
}

Upvotes: 8

Related Questions