简体   繁体   中英

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 code

    $('#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
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

This is the excel file output

You can strip all the tags from the array $_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.

Maybe like this: $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;
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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