Monday 3 September 2018

Publish and trigger a php file in a server using jquery ajax post method

send the values from a javascript file to server side php file and create a file in server side using the jquery ajax post method.

try some code
javascript code
  $.ajax({
    type: "POST",
    url: "http://localhost/export/some.php",
    data: { dataString: "hi" },
    cache: false,
    success: function(){
      alert(dataString);
      this.openDestinationURL();
    }
  });

php code as below
 header("Pragma: public");
 header("Expires: 0");
 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
 header("Content-Type: text/x-csv");
 header("Content-Disposition: attachment;filename=\"search_results.csv\""); 

if($_POST['data']){
  print $_POST['data'];
}


Try this it is working
$.ajax({
  type: "POST",
  url: "http://localhost/export/some.php",
  data: { dataString: "hi" },
  cache: false,
  success: function(data){
    alert(data);
    this.openDestinationURL();
  }
});

here is the php file
<?php
header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="sample.csv"');
$data =$_POST["dataString"];

$fp = fopen('php://output', 'w');//set the path
fputcsv($fp, $data);
fclose($fp);
?>

0 comments:

Post a Comment