Q:

jquery ajax post multi dimensional array to php

I am having a download as csv button for exporting the results as csv. Till now I was using server side code for this. I was sending the selected DropDowns Ids to csvdownload.php page which is writing the results in excel.
BUT NOW, I want a utility for selecting the search results using checkbox and then the selected results should written to the csv. 
I am planning to get the table data in array and then post it to php using ajax, then it will do the rest of things. But how is this possible to post multiple rows of data to php. I have following piece of code for getting the table data which is alerting data but dont know how to make it as array and pass to php
      $('#tblSearchResults tr').filter(':has(:checkbox:checked)').each(function() {
     $tr = $(this);
       //alert($tr.find("td").eq(4).html());

        var risk_cat = $tr.find("td").eq(1).html();
        var risk_type = $tr.find("td").eq(2).html();
        var risk_desc = $tr.find("td").eq(3).html();
        var treat_desc = $tr.find("td").eq(4).html();
        var treat_cat = $tr.find("td").eq(5).html();

        selectedData.push({"risk_cat": risk_cat, "risk_type": risk_type, "risk_desc": risk_desc, "treat_desc": treat_desc, "treat_cat": treat_cat});

thanks

A:

I think you need to use JSON.stringify(selectedData) in order to use it on the serverside.
jQuery:    
var obj = { 'risk_cat': risk_cat, 'risk_type': risk_type };
selectedData.push(obj);

$.post('serive.php', { DTO: JSON.stringify(selectedData) }, 
function(data){ /* handle response,  */ });

service.php:
header('Content-type: application/json');
header('Cache-Control: no-cache, must-revalidate');

$foo = json_decode($_POST['DTO']);

$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); //example data

echo json_encode($arr);

This should get you started. In your ajax reponse, alert(data.a) would be alerting "1"

