tags:

views:

137

answers:

3

I'm loading an array in my javascript. I need to send this array, in some format, to the PHP script that I'm going to call. In my example below, gSelectedMeds is my array. The value "count" will tell the PHP script how many "meds" items to expect to receive. I'm having trouble getting the data from the array into an format that I can send via the data option of $.ajax. Any help would be greatly appreciated!!

The part of the code below that is giving me grief at the moment is the data option.

$('#export').click(function() {
    $.ajax({
     url:'ajax-exportMeds.php',
     data: 
      {"number":gSelectedMeds.length},
      $.each(gSelectedMeds,
       function(intIndex, objValue){
        {"med"+intIndex:objValue},
       }
      ),
     type: "GET",
     //dataType: "text",
     success: function(data){
      $('p#allMeds').text('');
      $('a.bank').text('');
      //clear array, bank and storedList divs
      $(this).text('');
      gSelectedMeds[] = '';
      //$('ul#storedList').fadeOut('fast');
      $('ul#storedList').text('');
      return false;
     },
    }),
});
+4  A: 

You should send the data as json. Then you can read it using json_decode() in php >= 5.2.0

gnud
do I need to create the JSON up front? how would I send it using the `data` option?
acedanger
actually, I just used 'JSON.stringify' and that seems to have worked so far
acedanger
A: 
acedanger