I am trying to allow users to save progress by saving an array of data to a file. I know I can force a php download by changing the headers, but I am calling the php script with an ajax call that needs a response to re-enable a button.The reason I neeed to disable the button in the first place is jquery has to gather ALOT of data to send to the script and it takes a couple of seconds to do. I have posted my code below which just saves a local file. Ideally I would just like a “save to” dialog and let ajax know it wen ok.
So could I force a download and still echo back the json data response?
Thank you once again,
Todd
require_once('../FirePHPCore/fb.php');
session_start();
/* Initialize an ajax pessimistic response object. */
$ResponseData = array
(
'Success' => false,
'Message' => 'There was an error saving your chart right now. Please try again later.'
);
// assemble the final array to save to disk
$finalArray = array();
$finalArray['colorChart'] = $_POST['colorChart'];
$finalArray['threadBrand'] = $_POST['threadBrand'];
$finalArray['chartName'] = $_POST['chartName'];
$finalArray['stitchChart'] = $_POST['stitchChart'];
file_put_contents('array.txt', serialize($finalArray));
$ResponseData['Success'] = true;
$ResponseData['Message'] = 'Saved.';
/* Output the response data in JSON. */
echo json_encode($ResponseData);
Your AJAX call can have PHP prepare the file for download, then return the filename of the temporary file.
Then, the JavaScript can redirect the browser to the temporary file. If appropriate headers are set, the file will be downloaded and the browser window remains unchanged.