dynamic fields

nratter 23 Oct, 2012
I got a test code running to clone div's. I have been having a major problem getting the values of the cloned fields to show up on submit. Looking at the debugger, it just sends the data from the last cloned row. I have been lurking the forums for hours trying to find a fix to this. I know its been touched on before, but I haven't seen a solution to getting the cloned fields to show up in the output.

Here's my javascript code that's loaded in the 'on load' event...


window.addEvent('domready', function() {
  $('hoo_addline').addEvent('click', function() {
    var newclone = $('cf_container_65').clone().injectAfter('cf_container_65');
    newclone.setAttribute("name", "testClonedInput");
    var testget = newclone.getAttribute("name");
    alert(testget);
  });
});


can i just fix this with php in the 'on submit' event?

ps - i know my attributes don't increment yet... i just want to see this work first before I start improving the code.

Thanks in advance!!
Nick
GreyHead 23 Oct, 2012
Hi Nick,

As you are only getting the last set to data the problem is that the name of he input isn't changing. There are a few possible solutions to this that I can think of:

a) one is to use an array name like name='some_input[]'. The problem with this is that you may have to untangle the array results afterwards.

b) another is to keep a counter and increment it for each new clone so you get name='some_input_4'

c) the best may be the hybrid solution to use an array with a counter name='some_input_group[4][some_name]' This keeps each cloned 'input group' together inside the results array.

Bob
nratter 24 Oct, 2012

Hi Nick,

As you are only getting the last set to data the problem is that the name of he input isn't changing. There are a few possible solutions to this that I can think of:

a) one is to use an array name like name='some_input[]'. The problem with this is that you may have to untangle the array results afterwards.

b) another is to keep a counter and increment it for each new clone so you get name='some_input_4'

c) the best may be the hybrid solution to use an array with a counter name='some_input_group[4][some_name]' This keeps each cloned 'input group' together inside the results array.

Bob



Finally got it working!
In DB Save (in cf v4), there is a 'parameter fields' option at the bottom of the config page that says, underneath: "List of form fields names which will be treated as Joomla Parameters fields when processed, those fields should be always of type Array" Could I use this to save all cloned fields created and renamed (as in solution C you gave above) to separate entries in a single database table?

Nick
GreyHead 24 Oct, 2012
Hi Nick,

Parameter fields are a specialised way of storing data. It's used by Joomla! for storing bundled data e.g. the user's time-zone and language preferences: For example:
{"admin_style":"","admin_language":"","language":"","editor":"codemirror","helpsite":"","timezone":""}
This is now a JSON array, in Joomla! 1.5 it was an ini file structure.

Unless you are comfortable with this kind of format I suggest that you don't use it. The ChronoForms Handle Array action and the Array settings in other actions should do what you need.

Bob
nratter 24 Oct, 2012
But will this allow me to save multiple entries in a table with a single 'db save' ? Since the fields are dynamically added, I don't have a predefined number of fields. In other words, if the user adds 10 fields, I want this to save to 10 different records in the same table.

Thanks again for all your help!

Nick
GreyHead 24 Oct, 2012
Hi Nick,

No, it won't help with saving multiple records. None of the ChronoForms actions will do that but you can hand-code a MySQL query in a Custom Code action to do it. There's an example in this post

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