控制$ _POST中出现的变量

时间:2021-12-04 08:55:25

I have a form with several elements. When they submit the form, I want to use $_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?

我有一个包含几个元素的表单。当他们提交表单时,我想使用$ _POST来查看已选择的值。现在,是否有可能使它只有在选择不是0的情况下才实际被“发布”,因为0是默认值,如果它是0,它们实际上并没有选择任何东西?或者我是否必须通过整个$ _POST数组来查看哪些不为零?

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!

我不知道我想在$ _POST数组中访问哪些元素。我只希望我的表单提交页面(在action =“”部分中的页面)知道具有非零选择的字段。这可能吗?谢谢!

3 个解决方案

#1


5  

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.

在脚本执行开始之前设置环境时,将填充$ _POST,因此您无法控制其初始内容。它将包含所有表单数据。

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:

如果您不知道那里将包含哪些元素,只需遍历所有元素即可。您还可以轻松地从代码中删除数组中的0:

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

#2


0  

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

你可以添加一些javascript(jquery)来删除表单中的所有空输入,然后再将它们提交到你的php脚本

#3


0  

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?

您将依赖客户端表单过滤,这是完全不可靠的。除了每个字段浪费几个字节的带宽之外,“归零”字段被发送到服务器的问题是什么?

#1


5  

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.

在脚本执行开始之前设置环境时,将填充$ _POST,因此您无法控制其初始内容。它将包含所有表单数据。

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:

如果您不知道那里将包含哪些元素,只需遍历所有元素即可。您还可以轻松地从代码中删除数组中的0:

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

#2


0  

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

你可以添加一些javascript(jquery)来删除表单中的所有空输入,然后再将它们提交到你的php脚本

#3


0  

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?

您将依赖客户端表单过滤,这是完全不可靠的。除了每个字段浪费几个字节的带宽之外,“归零”字段被发送到服务器的问题是什么?