jquery调用php函数执行下载

时间:2017-07-19 12:51:49

标签: php jquery

我想实现:

以html格式输入一些数据(例如-wahl-和-id-) 点击-submit后调用javascript函数-machExport- 此函数验证值并将它们(-wahl-和-id-)传递给php-File -makeExport.php- -php-Funktion获取这些值,从mysql数据库获取一些数据,创建输出数据, 将这些数据写入文件-data.txt-并立即将此文件下载到用户download-folder。

一切都运作良好,但是: 创建数据后,此数据将存储到文件中,但不会下载。

那么,出了什么问题。 (如果我通过将一些值传递给php函数来直接执行-php文件,它可以很好地工作,

所以只有在html-和php-function之间存在javascript函数时,ist才能工作)

我的-html-文档中的部分代码

<form name="form" action="" method="post" onsubmit="return machExport()" onreset="abbruch()">

Javascript的代码:

function machExport(){
  var wahl    = $("input[name='rd1']:checked").val();
  var id      = $("#id").val();
  // verify these values and give an error message, if necessary
  ...
  // if everything is ok, pass values to the -php- function
  var ajxFile = 'makeExport.php';
  var param   = wahl + "#" + id;
  $.ajax({
    type: "POST",
    url: ajxFile,
    data: param
    // if I put here a -success: function -statement,
    // then the data having been created within my php-function
    // is not beeing written into the requested file
    // but is displayed in my html-document.
    }
  });

 return false;
}

PHP-File -makeExport.php -

<?php
// get the values for -wahl- and -id-
...
// create data to write into file
$data = ...
// write data to file and download this file instantly
$file = "data.txt";
$fOK = file_put_contents ( $file , $data );
// up to here, it works
// but the next 3 lines are not excecuted
header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
?>

0 个答案:

没有答案
相关问题