如何通过AJAX从PHP脚本下载CSV文件?

时间:2020-06-24 23:36:03

标签: php jquery ajax csv

因此,我想下载单击按钮后由我的PHP文件创建的CSV文件。

以前,这很容易,就像我说的那样

<form action="working_csv.php method="post">

它会将用户重定向到“新页面”并下载我的CSV文件。

这是“ working_csv.php”内部的样子。

I use the $_POST['location'] and $_POST['filter'] data 
earlier in the file, but it's too big of a file, so I omitted that part.

  echo "Amount of entries: ".(sizeof($myarray))."\n";

  $output = print_r($myarray, true);

  $filename = "output.csv";
  header('Content-Type: text/csv');
  header('Content-Disposition: attachment; filename="' . $filename . '"');
  header("Content-Length: " . strlen($output));
  echo $output;
  exit;

但是现在,我的表单中有2个按钮,一个用于在屏幕上显示输出,另一个用于下载与CSV文件相同的输出。

这是我home.php文件中当前代码的样子

  <div class="container">
    <form>      
      <h2><strong><p>Search the LDAP server</p></strong></h2>

      <label for="location"><p>Enter your search location (ex: dc=example,dc=com)</p></label>
      <input type="text" id="location" name="location" placeholder="Search Location..">

      <label for="filter"><p>Enter your search filter(s) (ex: uid=* or </p></label>
      <input type="text" id="filter" name="filter" placeholder="Filter(s)..">


      <input type="submit" name="search" id="search" value="Search LDAP Server" onclick="return chk()">

      <input type="submit" name="download" id="download" value="Download results as CSV file" onclick="return chek()">
    </form>
    <!-- Here's the AJAX call function. -->
    <p id="msg"></p>

  </div>
  <script>
    function chek()
    {
      var location=document.getElementById('location').value;
      var filter=document.getElementById('filter').value;
      var download=document.getElementById('download').value;
      var dataString={location:location, filter:filter, download:download};
      $.ajax({
        type:"post",
        url: "working_csv.php",
        data:dataString,
        cache:false,
        success: function(html){
          $('#msg').html(html);
        }
      });
      return false;
    }
  </script>

我的问题是,当调用我的AJAX函数时,它只是输出结果,而不下载CSV文件。

1 个答案:

答案 0 :(得分:1)

根据我的评论,我将使表格能够正常提交或使用Ajax提交。 Ajax搜索,正常提交下载:

<div class="container">
    <!-- Make this a proper form tag again -->
    <form id="ldap-form" method="post" action="working_csv.php">
        <h2><strong><p>Search the LDAP server</p></strong></h2>
        <label for="location"><p>Enter your search location (ex: dc=example,dc=com)</p></label>
        <input type="text" id="location" name="location" placeholder="Search Location.." />
        <label for="filter"><p>Enter your search filter(s) (ex: uid=* or </p></label>
        <input type="text" id="filter" name="filter" placeholder="Filter(s).." />
        <input type="submit" name="search" id="search" value="Search LDAP Server" />
        <input type="submit" name="download" id="download" value="Download results as CSV file" />
    </form>
    <!-- Response container -->
    <p id="msg"></p>
</div>

<script>
    $(function(){
       $('input[type="submit"]').on('click', function(e){
           // Stop form from submission
           e.preventDefault();
           // Detect button press type
           let thisSubmission   =   $(this).attr('name');
           // If the button is search, do ajax
           if(thisSubmission == 'search') {
                $.ajax({
                    type: "post",
                    url: $('#ldap-form').attr('action'),
                    data: $('#ldap-form').serialize(),
                    cache: false,
                    success: function(html){
                        $('#msg').html(html);
                    }
                });
           }
           else {
               // Just submit the form normally
               $('#ldap-form').submit();
           }
       });
    });
</script>