繁体   English   中英

如何使用 ajax 中的 get 方法仅将 html 代码“(不包括标签)”的文本部分传递给 php 页面

[英]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 文件,并且数据库表中有两行,它只从第二行导出一行和两列

这是 excel 文件 output

您可以从数组 $_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM