[英]How to pass only text part of html code `(excluding the tags)` to php page using get method from ajax
我想通過ajax將mysql數據導出到excel文件
Ajax代碼
$('#dateBox').change(function(){
$('#getData').html('loading...');
var date = $('#dateBox').val();
var limit = $('#sortByNo').val();
//set download button attributes
$('#exportSilver').attr('data-date',date);
if(date != ''){
var action = 'getDataFromDate';
$.ajax({
url: 'fetch_payouts.php',
method: 'post',
data: {date:date,action:action,limit:limit},
success:function(data){
$('#getData').html(data);
window.location.href = 'download.php?data='+data+'';
}
});
}
else{
$('#getData').html('');
}
});
下載.php文件
<?php
if(isset($_GET['data'])){
$data = $_GET['data'];
// The function header by sending raw excel
header("Content-type: application/vnd-ms-excel");
// Defines the name of the export file "codelution-export.xls"
header("Content-Disposition: attachment; filename=insway.xls");
echo $data;
}
?>
它可以工作,但問題是它還將 html 標簽導出到 excel 文件,並且數據庫表中有兩行,它只從第二行導出一行和兩列
您可以從數組 $_GET['data'] 中刪除所有標簽
嘗試以下代碼:
$data = array_map(function($v){
return trim(strip_tags($v));
}, $_GET['data']);
或者干脆
$data = array_map( 'strip_tags', $_GET['data'] );
您可以在回顯數據之前對數據使用 strip_tags function 的 PHP。
也許像這樣: $data = array_map(trim(strip_tags($data))
所以新代碼看起來像:
<?php
if(isset($_GET['data'])){
$data = $_GET['data'];
// The function header by sending raw excel
header("Content-type: application/vnd-ms-excel");
// Defines the name of the export file "codelution-export.xls"
header("Content-Disposition: attachment; filename=insway.xls");
$data = array_map(trim(strip_tags($data));
echo $data;
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.