user2102266
user2102266

Reputation: 539

opening dynamic php page with ajax

I have a php script that create user specific pdf files. (mpdf)

To download the file without losing the current page i used ajax.

var jsonString = JSON.stringify(multydimarray);
$.ajax({
  type: "POST",
  url: "gen.php",
  data: {data : jsonString}

  success: function(response){
   window.location = "gen.php";
  }
});

But gen.php wasn't received any data from ajax. $_POST['data'] wasn't set So,

File was downloaded, Current page stayed untouched, but the file was empty.

Any suggestion?

Upvotes: 1

Views: 59

Answers (1)

Fabricator
Fabricator

Reputation: 12772

specify the ajax request's contentType to application/json; charset=utf-8

$.ajax({
  type: "POST",
  url: "gen.php",
  data: {data : jsonString}
  contentType: "application/json; charset=utf-8",

  success: function(response){
   window.location = "gen.php";
  }
});

then get the data in PHP like this

$data = json_decode(file_get_contents('php://input'));

Upvotes: 1

Related Questions