Search code examples
phpjavascriptcsvdhtmlx

Pass CSV String from Javascript to PHP


I'm trying to export my DHTMLX Grid to a CSV file. I have the framework for it set up, but am running in an issue where if I try to export more than ~25 rows, it fails. No error message or anything. Below is my code:

Javascript

myGrid.csvParser = myGrid.csvExtParser;
myGrid.setCSVDelimiter('|');
myGrid.csv.row = "endOfRow";

var gridCsvData = myGrid.serializeToCSV();

$.post("data/export.php?csvdata="+gridCsvData);

PHP

$csvData = $_REQUEST['csvdata'];

$csv = explode('endOfRow',$csvData);

$myfile = "grid.csv";

$fh = fopen($myfile, 'w') or die("can't open file");

foreach($csv as $line) {
   fputcsv($fh, explode('|',$line),',','"');
}

fclose($fh);


//Redirect output to a client's web browser (csv)
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=grid.csv");
header("Pragma: no-cache");
header("Expires: 0");

readfile('grid.csv');

Any help is appreciated. I assume it's some kind of size limit problem on the POST and I've tried modifying the limit to 20MB in the .htaccess file. As I said, this works perfectly with only a few rows (<25), but once I try to export any more than that it never generates the .csv file.


Solution

  • I guess that $.post (from jQuery right?) doesn't handle such a huge url. You should add a parameter that can be handle as parameter of the HTTP POST query:

    $.post(
      "data/export.php", 
      { 
        csvdata: gridCsvData
      }
    );