TransWikia.com

How to Submit detail page (edit.php) with random amount of data?

Joomla Asked on December 19, 2021

On my detail page edit.php I have a list of records. The user can select records by a checkbox and the number of selected records is not fixed, I mean it can be one record, but also one time 5 or 11 records.

The total list of records can be 3 or 500 records or even more, so I don’t want to put this in the form and submit the whole bunch! I looks not an good idea to me.

I try give an idea how I see it like an array

        "rec1": {
                    "value1": "some data"
                    "value2": "some data"
                    "value3": "some data"
                    "value4": "some data"
                }
        "recn": {
                    "value1": "some data"
                    "value2": "some data"
                    "value3": "some data"
                    "value4": "some data"
                }
    }

I have to save these value’s as record in certain table. This I have this principle working, simply with the form fields one-value and different number of records.

Now I want to extend this with more data.
My question is, I’m looking for an tutorial or example of how do I:

  1. If I press the “Save” button, how can I submit these record value’s to the controller to save them?

  2. At the controller site how to pick up the records and extract values to save again?

What is the best way how to do that? application/Json or application/XML or application/x-www-form-urlencoded? Till now I don’t when or why is the one or another used?

I hope that someone can help me further and help me out so that I at least get an idea how to solve this.

Best Regards,

Nico

2 Answers

I think I understand the 'misunderstandnis' and I will try to make it more clear.

I have edit.php with list of records (it is more ore less an admin view of an another table). The user can select 1 or more 'records' by a checkbox and add some additional data to this record. This is data for a link table and not for the admin view I mention here before

I have made a filter bar, so the user can filter the list of records, which I have created with Ajax.

Add the moment the user decide to press the 'save' button, not all records are on the moment on the form because of the filter. On this moment I keep track of the selected records by putting the record id in a hidden field without the additional data, only to get the principle working!

But now the case I think were it turns around: So at the client side, before the user press the save button, I must have 'secured' somewhere the selected record id with the additional data!

Now, I think, the only way to do this is, to put it in an hidden field like this and I will try this:

'add_array' => string '{ "add_Array0":{"key0":"value0","key1":"value1","key2":"value2","key3":"value3"}, "add_Array1":{"key0":"value0","key1":"value1","key2":"value2","key3":"value3"} "add_Arrayn":{"key0":"value0","key1":"value1","key2":"value2","key3":"value3"}}'

With <?php echo JHtml::_('grid.id', $i, $item->id); ?> I'm to 'late', because it is server sided and what I mention, not all records are on the form on the moment of pressing the save button of the details page (edit.php).

(Besides this: the functionality around selecting, deselecting records which mean maintaining the hidden field with ONLY record id, I have working. Also saving it in the database with insert new records, update records, remove records. That's not the issue.)

I hope that I have it made more clear what I'm working on. The reason why I'm asking it here is that I'm curious if there is an 'mechanism' in Joomla or a smarter way to solve this, because I can't believe that I'm the first one with this issue.

And I'm also curious, it is the backend, but can it be an security issue to put the data on this way in the hidden field?

Thanks in advance,

Nico

Answered by user2363969 on December 19, 2021

I haven't verified what's below, but I think it should work.

I suggest you have a look at the Joomla Content Articles form, where you can select multiple articles via the checkboxes and then perform a batch operation, such as publishing / unpublishing all these. In particular look at administrator/components/com_content/views/articles/tmpl/default.php.

Another example is in https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Adding_backend_actions in the admin/views/helloworlds/tmpl/default.php code.

Basically you want to use something like what is in the com_content articles layout file:

 <?php echo JHtml::_('grid.id', $i, $item->id); ?>

for each of the records, which will output a checkbox with the html "name" attribute set to "cid[]" which will cause the ids to be sent in the http post as an array, so you can easily get these into a php array in your controller.

See https://api.joomla.org/cms-3/classes/JHtmlGrid.html#method_id for the API details (including eg how you can change the "cid" name).

Also include a hidden field

<input type="hidden" name="boxchecked" value="0" />

which will give you the number of records checked.

Answered by Robbie Jackson on December 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP