I'm trying to use the each function to post multiple fields, What I have is my regular fields, which submit and add to the db fine.
A function that clones the field when the user reaches near the end of filling the form in and one submit button.
I am trying to use $.ajax to submit the from values once each for each form on the page, it only seems to be working for the first form, any duplicated forms data isn't added which has me thinking I probably am using .each wrong could someone explain how I use it in this situation? Here's my attempt
$(function(){
var i = 0; var x = 0;
$.datepicker.setDefaults($.datepicker.regional['']);
$.datepicker.formatDate( "yy-mm-dd");
$('#datepicker' ).datepicker();
$('.vat').each(function(i){
$(this).click(function(){
var id = "batchinvoice" + x.toString();
$('#batchinvoice').clone().attr("name", id).appendTo(".panel-body");
x++;
});
});
$('#submit').click(function(){
var propid = Array();
var date = Array();
var ref= Array();
var id= Array();
var desc= Array();
var vat= Array();
var net= Array();
$('#batchinvoice[name*="batchinvoice"]').each(function(i){
propid[i] = $('#sl_propid').val();
date[i] = $('.sl_date datepicker').val();
ref[i] = $('#sl_ref').val();
id[i] = $('#sl_nom_id').val();
desc[i] = $('#sl_desc').val();
vat[i] = $('#vat').val();
net[i] = $('#sl_net').val();
i++;
});
$.ajax({
type: 'POST',
url: '<?php echo base_url(); ?>FinancialInput/addInvoiceToLedger',
data: { propid : propid, date:"date", ref: ref, id: id, desc: desc, vat: vat, net: net},
sucess: function(e){
alert(e.error);
},
error: function(e){
alert(e.error);
}
});
});
});
.vat Is the field that triggers the clone #submit is the one submit button and .batchinvoice is the class of each cloned form (and the original).
Everything I have read regarding this have been basic tutorials, any help appreciated, Cheers for reading :)