如何从HTML表单发布向PHP提交空数组 [英] How to Submit empty array from HTML Form Post to PHP

查看:78
本文介绍了如何从HTML表单发布向PHP提交空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚意识到-我不知道如何将一个空数组从HTML表单发布到PHP.我可以发布大小为1或更大的数组,但是有什么方法可以发布空数组?我的意思是,您可以拥有:

I just realized - I have no idea how to POST an empty array from an HTML form to PHP. I can POST arrays of size 1 or larger, but is there any way to post an empty array? I mean, like, you can have:

<form>
  <input type='hidden' name='items[0]' value='Value Item 1' />
 <input type='hidden' name='items[1]' value='Value Item 2' />
</form>

给出一个大小为2的数组:$_POST['items'] === ["Value Item 1", "Value Item 2"],但是有什么方法可以传递一个空数组作为$ _POST键的值?也就是说,我知道我可以创建$POST['items'] === '',但是可以创建一个空数组([])?

which gives an array of size 2: $_POST['items'] === ["Value Item 1", "Value Item 2"] but is there any way to pass an empty array as the value of a $_POST key? That is, I know I can make $POST['items'] === '', but an empty array ([])?

特定的用例是:我有一个一对多的表格,比如说一个购物车,上面有物品.该表单由数据库中现有的项目"填充,但是可以添加或删除项目数组中的项目.项目数组已建立索引-如果添加了新项目,则将它们保存到数据库;如果删除了项目,则将它们从阵列中删除并从数据库中删除.

The specific use case is: I have a one to many form, say like a shopping cart with items. The form is populated by existing "items" from the DB, but can add or delete items, which are in an array of items. The array of items is indexed - if new items are added, they are saved to the DB; if items are deleted, they are removed from the array and deleted from the DB.

所有这些都可以正常工作-除非从表单中删除所有项目,否则不会传递"item"键.

All of which works fine - unless all the items are deleted from the form, in which case no "item" key is passed.

我找到了一个很好的解决方法,即在表单顶部放置一个隐藏的输入"items" = null,因此即使没有任何项目也要传递键,然后在PHP中将"items" 键是"我将其转换为空数组-因此一切正常.从理论上讲,我只是好奇是否可以发布一个空数组.不知道为什么认为这是不合适的-希望解释会有所帮助.

I found the perfectly good work-around, which is to put a hidden input "items" = null at the top of the form, so the key is passed even if there are no items, then in PHP if the "items" key is '' I convert it to an empty array - so all works fine. I was just curious in theory if it was possible to POST an empty array. No idea why that is considered inappropriate - hope the explanation helps.

至于为什么我要检查密钥的存在-我使用相同的通用代码来处理来自不同形式的输入-因此,如果用户更改购物车上的地址而未指定项目,则这些项目应不会因为没有"items"键而被删除.

As for why I want to check on the existence of the key - I use the same generic code to process inputs from different forms - so, if the user changes the address on the shopping cart without specifying the items, the items should not be deleted just because there is no "items" key.

为了进一步说明,我实际上不是在构建购物车",而是在构建一个抽象框架,该框架支持将一对多POST映射到雄辩的一对多关系. POST中具有空值的任何字段都将从对象中删除-但是POST中未包含的任何字段/键都不会在基础DB中删除或修改.

To clarify further, I'm not actually building a "Shopping Cart" - I'm building an abstract framework that supports mapping one-to-many POSTs to Eloquent One-To-Many relationships. Any field in a POST with an empty value is deleted from the Object - but any field/key NOT in the POST is NOT deleted or modified in the underlying DB.

重点是,我正在构建一个通用的工具集-不仅仅是试图让一个函数来处理一种形式.

Point is, I'm building a generic tool-set - not just trying to get one function to handle one form.

推荐答案

不,您不能(尽管了解它似乎毫无意义).

No, you can't (although it seems pointless to know it).

一旦设置了一个数组类型的表单域,它所产生的索引就和它所具有的值一样多.在那种情况下,它是一个空字符串,意味着它的值什么也不是(''),只是因为它存在. 对于PHP,没有显式值意味着为null或为空,这将算作有效值. 没什么表示根本没有声明或定义,这不是空数组或空值数组的情况.

Once you set an array-type form field, it yields as much indexes as there are values to this. In that case, it's an empty string, meaning its value was nothing (''), just because it exists. To PHP, having no explicit value means being something null or empty, which counts as valid value. Nothing means not being declared or defined at all, which is not the case of an empty array, or an array of empty value(s).

<form method="POST" id="test" action="test.php">
	<input type='hidden' name='items[]' />
</form>
<input type="submit" value="Send" form="test">

print_r($_POST)

OUTPUT of print_r($_POST)

Array
(
    [items] => Array
        (
            [0] => 
        )

)

如果您希望$_POST是一个空数组,则不要通过POST方法发送任何内容.超全局变量在运行时设置为空数组.玩得开心. :)

If you want $_POST to be an empty array, just don't send anything via POST method. The superglobal is set at runtime as an empty array. Have fun. :)

这篇关于如何从HTML表单发布向PHP提交空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆