开发者

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 开发者_StackOverflowempty set of data has been POSTed to a page in PHP?


Use the empty function

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


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


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


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


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

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


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


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

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


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
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜