function dadosFormularios() {
var dadosFormulario = {};
var iterador = countForms;
var i = 0;
while (i < iterador) {
dadosFormulario[i] = {};
dadosFormulario[i]['a'] = $('#field\\[' + i + '\\]\\[a\\]').val();
dadosFormulario[i]['b'] = $('#field\\[' + i + '\\]\\[b\\]').val();
//alert(dadosFormulario[i]['a']);
//alert(dadosFormulario[i]['b']);
i++;
}
var qstring = '';
var tmp_qstring = [];
var temp1, temp2;
var aux_i = i + 1;
alert(aux_i);
for (var j = 0; j < aux_i; j++) {
temp1 = dadosFormulario[j]['a'];
alert(temp1);
temp2 = dadosFormulario[j]['b'];
//alert(temp1);
tmp_qstring[j] = 'a' + j + '=' + temp1 + '&' + 'b' + j + '=' + temp2;
}
qstring = tmp_qstring.join('&');
alert(qstring);
window.location = 'dup1.php?' + qstring;
}
When i click on a button i call the above function and now I’m trying to send the data of the array of objects to another page as you can see in the code.
My problem is that i’m getting trouble when i set dadosFormulario[j]['a']; or dadosFormulario[j]['b']; to a var or set them directly in the tmp_qstring i.e. tmp_qstring[j]=dadosFormulario[j]['b'];. In chrome console i get the error:
Uncaught TypeError: Cannot read property ‘a’ of undefined
Fix your loop to use:
Because both your array and your iterator variable
jare zero-based. You have to stop iteration when your iterator reaches array length – 1 (iis the length in your case). This is typically the case forforloops over the length of an array.If you iterate until
j <= iyour iteration index will go out of the end bound of the array.