php - 如何使用来自ajax的get方法仅将html代码的文本部分`(不包括标签)`传递给php页面
问题描述
我想通过ajax将mysql数据导出到excel文件
阿贾克斯代码
$('#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'] );
推荐阅读
- angular - Nginx 角度 Cors 问题
- dictionary - 如何防止字典键中的子字符串匹配?
- python - 我在 VS Code 上遇到了 python 导入错误
- amazon-eks - eks Anywhere 集群创建卡住
- jenkins - 在詹金斯中创建多个节点需要另一种方法
- ruby - 访问我的乳齿象网站时,我得到阻止域的网页
- r - 在包含 R 中日期的列中嵌套 ifelse
- google-cloud-platform - 减少 GCP 堆栈驱动程序中的警报噪音
- android - 检测何时没有视图集中在 Android 活动中
- vba - VBA OAuth:在 VBA 中获取 OAuth 代码和状态