I have a script for exporting results of a mysql query as a csv file. The thing is that i use jquery to access the script. I need to know how i can return the data (already in csv format) with jquery. In other words, just to make myself clear because my english is a bit poor, when the user presses a button in the html file, a post request is sent to a php file which returns the data in csv format. I want to take that data with jquery and serve the .csv file to the user.
PHP file:
<?php
session_start();
header ("Content-type: application/csv\nContent-Disposition: \"inline; filename=my.csv\"");
include("config.php");
$query = $_SESSION['sqlQuery'];
$result = mysql_query($query) or die("Query failed : " . mysql_error());
echo "ID,STATUS,CATEGORY,TITLE,DATE,URL\r\n"; //header
while($row = mysql_fetch_row($result)) {
echo "$row[0],$row[4],$row[3],$row[1],$row[2]\r\n"; //data
}
?>
I need something in the HTML like:
$("#exportToCsv").click(function(){
$.post("export.php",function(data){
here the code for exporting much like downloading a file
});
});