2

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);
Joel
  • 380
  • 3
  • 16
  • What you have in `$_POST['data']` have you tried to dump it ? in `export.php` just write `var_dump( $_POST['data'] )` and see what it gives you – Armen Dec 14 '15 at 11:50
  • Can you please post your form used to post the data? Your error say that You don't have any index named 'data' in your $_POST array. – Franco Dec 14 '15 at 11:52
  • Why do you want to navigate to export.php ` window.location.replace("export.php");`? You got the response right? When you call this, your post data is empty, so does return it – Thamilhan Dec 14 '15 at 11:55
  • Why not just use php post if you will go to export.php? – momouu Dec 14 '15 at 12:36
  • I have changed it so that i dont navigate to export.php, instead I send it to the same/current page (palettes.php). I have also removed the code window.location.replace("export.php"); – Joel Dec 14 '15 at 12:49

2 Answers2

3

In your code in this line:

 data: ({data: 'John'}),

try without ():

 data: {data: 'John'},
NormundsP
  • 445
  • 2
  • 7
  • 16
  • Have tried both with and without, no difference though. – Joel Dec 14 '15 at 12:36
  • What @Normis say is correct the data has to be passed only in brackets and not parenthesis. But the problem is not there. Which error message are you getting now. – Franco Dec 14 '15 at 12:55
0

Can you please post your form used to post the data? Your error say that You don't have any index named 'data' in your $_POST array. Anyway you can not check the existence of a value the way you are doing, the correct way is:

if (isset($_POST['data']))
{
...
}

And not:

$data = $_POST['data'];
if (isset($data))
{

In this way you are asigning the poste value to a variable and then checking against it

Please note this is only pointing to this issue and not solving the error you are facing. Post your HTML and we try to help you.

Franco
  • 2,309
  • 1
  • 11
  • 18