views:

92

answers:

4

I have a 2D array which I want to send to a php page with $.ajax.

This is the code which creates the array:

for (var i = 0; i<rowlen; i++) {
                           if (breakcheck) {
                              break;
                           }
                           for (var j = 0; j<=columnlen; j++) {
                              thtext = columnheads.eq(j).text();
                              current_td = $(newrows[i]).find("td").eq(j);

                              if (current_td.find("input").length >0) {
                                 rowdata[i,thtext] = current_td.find("input").val().trim();
                                 if (rowdata[i,thtext] =='') {
                                    alert("You must complete all fields");
                                    breakcheck = true;
                                    break;
                                 }
                              } else {
                                 rowdata[i,thtext] ='nada';
                              }
                           }//inner loop
                        }//outer loop

The array is filled properly with the nested loops and the I use JSON.stringify to format it. However when the ajax call is made all that is sent is an empty object ([]). What's wrong?

+2  A: 

I might be wrong, but arr[i,j] is not the way to use multidimensional arrays in C-style languages. That would be arr[i][j].

IMHO what arr[i,j] will do is function as comma operator and use only j as an index.

EFraim
arr[i][j] will not fill the array with data in my code.
flyon
A: 

I tried the following piece of code which uses the arr[i][j] notation. The array is filled with data, but again the data is not posted. Firebug reports this [[]] as the post data.

for (var i = 0; i<rowlen; i++) {
                           if (breakcheck) {
                              break;
                           }
                           for (var j = 0; j<=columnlen; j++) {
                              thtext = columnheads.eq(j).text();
                              current_td = $(newrows[i]).find("td").eq(j);

                              if (current_td.find("input").length >0) {
                                 r[thtext] = current_td.find("input").val().trim();
                                 if (r[thtext] =='') {
                                    alert("You must complete all fields");
                                    breakcheck = true;
                                    break;
                                 }
                              } else {
                                 r[thtext] ='nada';
                              }

                           }//inner loop
                           rowdata[i]=r;
                        }//outer loop
flyon
Can you show the AJAX call itself?
EFraim
var jar = JSON.stringify(rowdata);if(!breakcheck) { $.ajax({ type : 'POST', url:'change.php', data: jar, dataType: 'json'}); }
flyon
A: 

OK I solved this by declaring r as an object (var r = {}) instead of declaring it as an array (var r = []). Thanks for the help.

flyon
this should be in the original question.
Ape-inago
A: 

Also I had to redeclare r as an empty object inside the outer loop, because otherwise every row was filled with identical data.

for (var i = 0; i<rowlen; i++) {
r = {};
etc.

flyon (S.O. forgot my openid)