I wanna send values with ajax to php, save the values in a txt-file, and give the user the option to save the file.
I get the error message:
Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/randomColors/webroot/incl/theme.php:26) in /Applications/MAMP/htdocs/randomColors/webroot/palettes.php on line 26
What am I doing wrong?
function exportColors() {
$.ajax({
type: "POST",
url: "palettes.php",
data: ({data: 'John'}),
success: function (data) {
}
});
}
This is the code in export.php:
if (isset($_POST['data']))
{
$handle = fopen("file.txt", "w");
fwrite($handle, $_POST['data']);
fclose($handle);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;
}
The html involved is a div:
<div class="palettesDIV" data-id="220">
When it is clicked the value in "data-id" will be submitted to export.php with the exportColors-function. Right now I am only using {data: 'John'} as a placeholder.
The exportColors-function is trigged by this code (that is placed inside a for loop)
palettesDIVArray[x].addEventListener('click', exportColors, false);