简体   繁体   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

I want to export the mysql data to excel file through ajax我想通过ajax将mysql数据导出到excel文件

Ajax code 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('');
        }
    });

download.php file下载.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;
}
?>

It works but the problem is it also exports the html tags to the excel file and there are two rows in the database table and it only exports one row and two columns from second row它可以工作,但问题是它还将 html 标签导出到 excel 文件,并且数据库表中有两行,它只从第二行导出一行和两列

This is the excel file output这是 excel 文件 output

You can strip all the tags from the array $_GET['data']您可以从数组 $_GET['data'] 中删除所有标签

try following code:尝试以下代码:

$data = array_map(function($v){
    return trim(strip_tags($v));
}, $_GET['data']);

Or simply或者干脆

$data = array_map( 'strip_tags', $_GET['data'] );

You can use strip_tags function of PHP on data before echoing it.您可以在回显数据之前对数据使用 strip_tags function 的 PHP。

Maybe like this: $data = array_map(trim(strip_tags($data))也许像这样: $data = array_map(trim(strip_tags($data))

So the new code looks like:所以新代码看起来像:

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

相关问题 如何在不丢失html标签的情况下获取大文本的一部分? - How to get part of a big text without losing html tags with php? 如何使用ajax从html获取数据并将其传递到php - How to get and pass data from html to php using ajax 如何在网页中将 php 代码和 html 标签显示为文本 - How to display php code and html tags as text in a web page 如何在新链接中传递部分url? 仅使用HTML和PHP - How to pass part of url in new link? Using only HTML & PHP 我想使用按钮单击功能上的Ajax代码将多个值从不同的div标签传递到另一个php页面 - I want to pass multiple values from different div tags to another php page using Ajax code on button click function 如何使用AJAX将用户输入值从PHP页面传递到HTML页面 - How to pass user input value from a PHP page to HTML page using AJAX 如何从服务器获取带有HTML标记的文本并通过HTML标记在客户端显示它? (使用php和angularjs) - How get text with HTML tags from server and display it in client side with HTML markup? (using php and angularjs) 如何使用 AJAX 将 PHP 数组传递到 HTML 页面? - How To Pass PHP Array to HTML Page using AJAX? 如何使用 PHP 从 ajax 响应中删除 HTML 标签? - How to remove HTML tags from ajax response using PHP? 如何将带有&符号的文本从ajax传递到php代码? - How can I pass text with & symbol from ajax to php code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM