开发者

Control what variables appear in $_POST

I have a form with several elements. When they submit the form, I want to us开发者_开发问答e $_POST to see what values have been selected. Now, is it possible to make it so that they only actually get "posted" if a selection is made that is not 0 since since 0 is the default value and if it is 0 they are not actually selecting anything? Or do I have to go through the entire $_POST array to see which ones are non zero?

I don't know which elements I want to access in the $_POST array before hand. I only want my form submission page (the page that goes in the action="" part) to be aware of fields that have non zero selections. Is this possible? Thanks!


No. $_POST is populated when the environment is set up before your script execution begins, so you don't control its initial contents. It will contain all of the form data.

If you don't know what elements will be there, just loop over all of them. You can also easily remove the ones that are 0 from the array in your code:

foreach ($_POST as $key => $value) {
    if ($value == 0) {
        unset($_POST[$key]);
    }
}


you could add on some javascript (jquery) to remove all null inputs from your form before they are submitted to your php script


You'd be depending on client-side form filtering, which is utterly unreliable. Other than wasting a few bytes of bandwidth per field, what's the problem with "zeroed" fields getting sent to the server?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜