第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過 AJAX 從 PHP 腳本下載 CSV 文件?

如何通過 AJAX 從 PHP 腳本下載 CSV 文件?

PHP
拉風(fēng)的咖菲貓 2023-07-01 13:08:42
所以我想下載一個 CSV 文件,該文件是通過單擊按鈕由我的 PHP 文件創(chuàng)建的。以前,這很容易,因為我可以直接輸入<form action="working_csv.php method="post">它會將用戶重定向到“新頁面”并下載我的 CSV 文件。這是“working_csv.php”的內(nèi)部結(jié)構(gòu)。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;但現(xiàn)在,我的表單中有 2 個按鈕,一個用于在屏幕上顯示輸出,另一個用于將相同的輸出下載為 CSV 文件。這是我的 home.php 文件中當(dāng)前代碼的樣子 <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>我的問題是,當(dāng)我的 AJAX 函數(shù)被調(diào)用時,它只是輸出結(jié)果而不是下載 CSV 文件。
查看完整描述

1 回答

?
夢里花落0921

TA貢獻(xiàn)1772條經(jīng)驗 獲得超6個贊

根據(jù)我的評論,我將使表單能夠正常提交或使用 ajax 提交。Ajax進(jìn)行搜索,正常提交下載:


<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>



查看完整回答
反對 回復(fù) 2023-07-01
  • 1 回答
  • 0 關(guān)注
  • 148 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號