0

I am attempting to get around having to raise the max_input_vars parameter when submitting a large form. My thought was to jsonify the form, create a second form with a single field in it, put the jsonified string into that single field, submit, decode as an array, and then replace the POST array with the decoded data. I am plugging this into an existing system so for this to work I would need the new array to have the same structure as the raw post would. However, I am running into an issue where regardless of how I encode the data I am losing info and structure beyond the first depth of the array. I have tried both using this library to encode the form:

https://github.com/macek/jquery-serialize-object

As well as jQuery(form).serializeArray(), each of which delivers results that decode in their own unintended ways. This is what the raw post data looks like when I don't intercept the form and dump it with print_r($_POST):

Array
(
    [closedpostboxesnonce] => 9d9dc8fa74
    [meta-box-order-nonce] => 520ef5d263
    [update-nav-menu-nonce] => cfea78920d
    [_wp_http_referer] => /wp-admin/nav-menus.php
    [action] => update
    [menu] => 2
    [menu-name] => Left Sidebar Menu
    [save_menu] => Save Menu
    [qtranslate-fields] => Array
        (
            [menu-item-title] => Array
                (
                    [3871] => Array
                        (
                            [en] => Knowledge Center
                            [ja] => Knowledge Center
                            [ko] => Knowledge Center
                            [zh] => Knowledge Center
                            [qtranslate-separator] => [
                        )

The data when I use the jquery-serialize-object library above to encode the form data, then decode it with json_decode($_POST["allinputs"],true):

Array
(
    [closedpostboxesnonce] => 9d9dc8fa74
    [meta-box-order-nonce] => 520ef5d263
    [update-nav-menu-nonce] => cfea78920d
    [_wp_http_referer] => /wp-admin/nav-menus.php
    [action] => update
    [menu] => 2
    [menu-name] => Left Sidebar Menu
    [qtranslate-fields[menu-item-title][3871][en]] => Knowledge Center
    [qtranslate-fields[menu-item-title][3871][ja]] => Knowledge Center
    [qtranslate-fields[menu-item-title][3871][ko]] => Knowledge Center
    [qtranslate-fields[menu-item-title][3871][zh]] => Knowledge Center

Using jQuery(form).serializeArray() to encode, and $allinputs = (array)(json_decode($_POST["allinputs"])); to decode:

Array
(
    [0] => stdClass Object
        (
            [name] => closedpostboxesnonce
            [value] => 9d9dc8fa74
        )

    [1] => stdClass Object
        (
            [name] => meta-box-order-nonce
            [value] => 520ef5d263
        )

    [2] => stdClass Object
        (
            [name] => update-nav-menu-nonce
            [value] => cfea78920d
        )

Is there a way to get the data encoded and then decoded while retaining the original structure?

Edit: using @think-win-win 's suggestion of passing true as the second parameter of json_decode yields:

Array
(
    [0] => Array
        (
            [name] => closedpostboxesnonce
            [value] => 9d9dc8fa74
        )

    [1] => Array
        (
            [name] => meta-box-order-nonce
            [value] => 520ef5d263
        )

    [2] => Array
        (
            [name] => update-nav-menu-nonce
            [value] => cfea78920d
        )

It is one of the ones that I tried, along with a couple of others, but I felt the post was getting too long to include them all. The above one loses the associativeness of the top level of the array, in addition to converting the deeper levels to:

[7] => Array
    (
        [name] => qtranslate-fields[menu-item-title][3871][en]
        [value] => Knowledge Center
    )

[8] => Array
    (
        [name] => qtranslate-fields[menu-item-title][3871][ja]
        [value] => Knowledge Center
    )
Michael VanDeMar
  • 157
  • 1
  • 1
  • 10

0 Answers0