简体   繁体   中英

How to Submit empty array from HTML Form Post to PHP

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>

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.

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

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.

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.

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).

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

OUTPUT of print_r($_POST)

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

)

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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM