Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery ajax post方法在服务器中发布并触发php文件_Php_Javascript_Jquery_Ajax - Fatal编程技术网

使用jquery ajax post方法在服务器中发布并触发php文件

使用jquery ajax post方法在服务器中发布并触发php文件,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,将javascript文件中的值发送到服务器端php文件,并使用jquery ajax post方法在服务器端创建一个文件 尝试一些代码 javascript代码 $.ajax({ type: "POST", url: "http://localhost/export/some.php", data: { dataString: "hi" }, cache: false, success: function(){ alert(dataStr

将javascript文件中的值发送到服务器端php文件,并使用jquery ajax post方法在服务器端创建一个文件

尝试一些代码

javascript代码

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

 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'];
}
试试这个,它正在工作

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

<?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);
?>

由于您正在发送
数据:{dataString:“hi”}
,因此实际的POST数据将是:

if(isset($_POST['dataString'])){
  print $_POST['dataString'];
}
也可以使用isset()

至于您的AJAX:

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

以多维数组形式发送数据

$.post('http://localhost/export/some.php', 
    {data: [["aaa","bbb","ccc","dddd"],["123","456","789"],["aaa","bbb"]]}, 
    function(){
        alert('saved');
    }
);
在php端,只需保存到文件

$output = fopen('path to csv file', 'w');
foreach ($_POST['data'] as $row) {
    fputcsv($output, $row, ';');
}
fclose($output);

如何查看php文件中的数据..在触发php文件时显示一些错误..将帮助我如何查看php文件@SarathSprakashCheck上面的代码我添加了php文件ok。希望这能帮助我在服务器端创建并保存csv文件。将帮助meI更新代码。看看这个。sample.csv每次都会被覆盖。如果您希望它附加,然后在附加模式下打开文件我想在服务器端保存csv文件,所以更改“csv文件路径”,文件将保存在服务器上。当您不想直接从web下载文件时,您不需要标题。然后,您可以使用直接链接到保存的文件进行下载。谢谢,但多维文件的格式正确。请设置如何在csv文件中正确插入。帮助我。数据的来源是什么?您不需要将其作为数组发送,只需将数据作为字符串发送,然后在php文件中对其进行解析。