Forums

form->data arrays not preserved in the submit event

tshirley 26 Sep, 2016
Hello,

I have read data into an array "C" in the Load event, in order to populate a dynamic dropdown. Then I render the html, the user fills the form and selects the item from the dropdown, and clicks submit.

However I want the contents of the array to be present in the Submit event, so I placed a hidden field with the field name C[] on the form, expecting that this would automatically retain a copy of the array C in form->data.

But this didn't happen - the array doesn't persist and the hidden field remains empty.

The workaround is easy enough - read the data in again - but why is it necessary to do that?

Or am I missing something simple...as usual...

Cheers

Tim
GreyHead 26 Sep, 2016
Hi Tim,

I would expect that to work, please drag a Debugger action into the end of the On Load event, then load the form and post the debug results here.

Bob
tshirley 26 Sep, 2016
OK,

So I have read some data into an array in the load event and it works as expected:

Array
(
    [option] => com_chronoforms5
    [chronoform] => TestArray
    [C] => Array
        (
            [0] => Array
                (
                    [id] => 17
                    [service] => Training
                    [type_id] => 0
                )

            [1] => Array
                (
                    [id] => 18
                    [service] => Passenger Flights
                    [type_id] => 0
                )

            [2] => Array
                (
                    [id] => 19
                    [service] => Cross-Country Flying
                    [type_id] => 0
                )

            [3] => Array
                (
                    [id] => 20
                    [service] => Airforce Cadet Squadron
                    [type_id] => 0
                )

        )

)


And I place a hidden field C[] to try to capture it.

But in a Debugger after Submit I see this:

Array
(
    [option] => com_chronoforms5
    [chronoform] => TestArray
    [event] => submit
    [dropdown1] => 17
    [button2] => Submit
    [C] => Array
        (
            [0] => 
        )

)


Yo see that dropdown1 field contains the selected value, but the array C is empty.

So why does the array not persist into the submit, since it is in $form->data?

I can do it with a Data to Session at the end of the Load followed by Session to Data at start of Submit, but I just puzzled about why that is needed.

Cheers

Tim
GreyHead 26 Sep, 2016
Hi Tim,

I suspect that is because it's a multi-level array; you can either look up the data again; or you could save it to the User session and read it back on submit; or you could JSON-encode it before saving in the hidden input and decode after submit.

Bob
tshirley 26 Sep, 2016
Hi Bob,

Yes, after a bit more testing, there are three techniques for this:

1. Re-read the data
2. Use a Data to Session and Session to Data pair of actions on either side of the submit
3. Use json_encode and json_decode to pass the array as a simple field.

Many thanks as always.

Tim
This topic is locked and no more replies can be posted.