1

I am fairly new to JS and AJAX, and for some reason I can not send my dynamically generated and read data via AJAX. How do I properly send an array via AJAX to PHP script?

I have tried following the instructions but I can not get the AJAX to send the data. The first try was a complete bust, the second gets error:

Uncaught TypeError: Illegal invocation

but it seems to originate from the JS-library instead of my code (though the code is most probably the reason for it!) and I do not know what to do with it.

    //first thing I tried
    var i = 1, j = 0, cu = [], cu2 = [];
     while (i <= tilit ) {
        cu[j] = document.getElementById("til_nro_"+i);
        console.log(cu[j].value);

        i++;
    }
    var dynamic_account_value = JSON.stringify(cu);


 jQuery.ajax({
        type: "POST",
         url: 'http:mysite.php',
        dataType: 'json', 
        data: { dynamic_account_count:tilit, db:cu , id:id, result:dynamic_account_value
            }
        });



    //2nd thing I tried
    var i = 1, j = 0, cu = [], cu2 = [];
     while (i <= tilit ) {

        cu[j] = document.getElementById("til_nro_"+i);
        cu2.push(JSON.parse(cu[j].value));

        i++;
    }
    var tilinrot_lisa = JSON.stringify(cu2);


     jQuery.ajax({
        type: "POST",
         url: 'http:mysite.php',
        dataType: 'json', 
        data: { dynamic_account_count:tilit, db:cu , id:id, result:tilinrot_lisa
            }
        });
halfer
  • 19,824
  • 17
  • 99
  • 186
user1054844
  • 922
  • 5
  • 17
  • 34
  • 1
    [Possible duplicate](http://stackoverflow.com/questions/2013728/passing-javascript-array-to-php-through-jquery-ajax) – Jay Sep 10 '14 at 08:20
  • 1
    Take a look at `$.param()` http://api.jquery.com/jquery.param/ – T00rk Sep 10 '14 at 08:20
  • 1
    I think you are trying to stringify DOM nodes, try this instead `cu[j] = document.getElementById("til_nro_"+i).value;`. – dfsq Sep 10 '14 at 08:21
  • `http:mysite.php`? You probably wanted `http://mysite.php` – Ke Vin Sep 10 '14 at 09:10
  • Thanks everyone. I got the stringify to work. Do not quite know hy, I re-wrote the 2nd part of the script and now it works. – user1054844 Sep 10 '14 at 09:44

2 Answers2

1

First, give them something that makes selection easier, like a common class. Second, use jquery serialize

$.ajax({
  url : "foo",
  data : $(".bar").serialize(),
  success : function(response) {}
})
Chris Caviness
  • 586
  • 3
  • 10
  • Could not get serialize or stringify to work so tried the "untouched" cu as dynaamic:cu however when I read the send data and receive it I only get the last value. – user1054844 Sep 10 '14 at 09:26
  • I do not quite know how to serialize it, since I am generating the div id on dynamic content... – user1054844 Sep 10 '14 at 09:28
0

Try this code snippet, Try to send just one variable then go for another & use content type. u can use //console.log(JSON.stringify(cu)); to view what's inside.

jQuery.ajax({
   type: 'post',
   dataType: 'json',
   data: {your_var:JSON.stringify(cu)},
   contentType: "application/json"

});
valar morghulis
  • 2,007
  • 27
  • 34