如何使用ajax中的get方法将html代码的文本部分`(不包括标记)`传递给php页面
我想通过ajax将mysql数据导出到excel文件 Ajax代码如何使用ajax中的get方法将html代码的文本部分`(不包括标记)`传递给php页面,php,excel,ajax,export,Php,Excel,Ajax,Export,我想通过ajax将mysql数据导出到excel文件 Ajax代码 $('#dateBox').change(function(){ $('#getData').html('loading...'); var date = $('#dateBox').val(); var limit = $('#sortByNo').val(); //set download button attributes $('#e
$('#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
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;
}
?>
download.php文件
$('#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
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文件,并且数据库表中有两行,它只从第二行导出一行和两列
您可以从数组$\u GET['data'中删除所有标记 请尝试以下代码:
$data = array_map(function($v){
return trim(strip_tags($v));
}, $_GET['data']);
或者干脆
$data = array_map( 'strip_tags', $_GET['data'] );
您可以从数组$\u GET['data']中删除所有标记 请尝试以下代码:
$data = array_map(function($v){
return trim(strip_tags($v));
}, $_GET['data']);
或者干脆
$data = array_map( 'strip_tags', $_GET['data'] );
在回显数据之前,可以对数据使用PHP的strip_tags函数 也许是这样: $data=阵列映射(修剪(条带标记($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;
}
?>
您可以在回显数据之前对数据使用PHP的strip_tags功能
也许是这样:
$data=阵列映射(修剪(条带标记($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;
}
?>
trim
和strip\u标签
正在工作,但array\u map
不工作。它清除整个数据trim
和strip\u标签
正在工作,但array\u map
不工作。它清除整个数据trim
和strip\u标签
正在工作,但array\u map
不工作正在工作。它清除整个数据trim
和strip\u标签
正在工作,但array\u map
不工作。它清除整个数据