I have a form that allows users to automatically add blocks of fields to it. For various reasons these fields need to be associated with other fields in their blocks. I am accomplishing this by putting all my fields in each block in an array like you see below
<select name="items[0][item_type]" class="item_type">
<option value="0">Bulding / Landscaping</option>
<option value="1">Full / Thin Veneer</option>
</select>
<select class="select_custom" name="items[0][standard]">
<option value="0">Standard</option>
<option value="1">Custom</option>
</select>
This will give me back something like Array ( [0] => Array ( [item_type] => 0 [standard] => 1 ) )
which will allow me to easily handle the data on the back end.
The problem is that the array index must be specified in order for this to work. When I append a new block of fields like the one above I need a way to change the index on all the items[][fieldname]
.
Below is the code that I use to append the block of inputs to the document. They are all contained within a div so I simply clone that div and append it after the last one.
$('#more_fields').click(function(){
$('.field_group:first').clone(true).hide().insertAfter('.field_group:last').slideDown('slow');
var last = $('.field_group:last');
last.append(new_button.clone(true));
last.find('select').val([]);
last.find(".custom_products").css("display","none");
last.find(".unit_selection").css("display","none");
last.find(".landscape_selection").css("display","none");
last.find(".veneer_selection").css("display","none");
last.find(".comments_section").css("display","none");
last.find(".standard").css('display','none');
});