1

my downloaded table only prints headers and does not display any content. iam following the tutorial from here:http://webslesson.blogspot.in/2016/02/export-mysql-data-to-excel-in-php-php-tutorial.html

THE FOLLOWING PLUGINS i have tried which fails to render my huge data 1.clarketm(merges rows) 2.(tried but unable to render huge dat)https://github.com/kayalshri/tableExport.jquery.plugin

my HTML DOM IS (index.php)

<div id="allTables">
    <table border="2" width="100%">
                <tr>
                    <th width="30%">Name</th>
                    <th width="20%">Activity on Code Project (%)</th>
                    <th width="10%">Activity on C# Corner (%)</th>
                    <th width="10%">Activity on Asp Forum (%)</th>
                </tr>
                <tr>
                    <td>Sibeesh</td>
                    <td>100</td>
                    <td>98</td>
                    <td>80</td>
                </tr>
                <tr>
                    <td>Ajay</td>
                    <td>90</td>
                    <td>0</td>
                    <td>50</td>
                </tr>
                    </table>
</div>
<p id="exportexcel">EXport to Excel</p>

<script>
 $(document).ready(function () {
    $('#exportexcel').click(function(){
          var excel_data = $('#allTables').html();
          console.log(excel_data);
          var page = "excel.php?data="+excel_data;
          console.log(page);
          window.location = page; // here iam sending data to excel.php through get method
 });
</script>

my excel.php file is

<?php
  header('Content-Type: application/vnd.ms-excel');
  header('Content-disposition: attachment; filename='.rand().'.xls');
  echo $_GET["data"];
?>

--please help me what iam doing wrong thanks in advance

Ish
  • 2,085
  • 3
  • 21
  • 38
  • I am not sure the GET string really wants HTML in it and in particular the equals signs in your data will look like valid GET string separators. I think you might want to send you data via POST. Perhaps you should look at jQuery and $jquery.post(). – Paul Coldrey Sep 12 '16 at 07:08
  • @Paul i have seen the tutorial,which was working for them http://webslesson.blogspot.in/2016/02/export-mysql-data-to-excel-in-php-php-tutorial.html –  Sep 12 '16 at 07:09
  • 1
    you might need to url encode the excel_data – Terry Sep 12 '16 at 07:13
  • what is in console.log(excel_data); ? – Abhijit Jagtap Sep 12 '16 at 07:14
  • @Abhijit,it is something like this excel.php?data= ..... whole data
    –  Sep 12 '16 at 07:17
  • @furrie how can i do that please help me –  Sep 12 '16 at 07:18

2 Answers2

1

Activity on C# Corner (%) - "#" in this line is causing the problem. encoding this will resolve the issue in above code.

But its better to use POST as suggested.

Puhal
  • 52
  • 5
  • ,how come you are so accurate,i have removed Activity on C# Corner (%) and is working man –  Sep 12 '16 at 07:46
0

If you pass html code via get then you must encode it else you can use post method. following code would be useful to you.

<script type="text/javascript">
 var htmlString= document.getElementById(allTables).innerHTML;
    $.ajax({
        type: "POST",
        url: "excel.php",
    dataType:'html',// ***add this option!***
        data: {html:htmlString},
        success: function (data) {
            // this is executed when ajax call finished well
             alert('file has been saved');
             location.reload();
        },
        error: function (xhr, status, error) {
            // executed if something went wrong during call
            if (xhr.status > 0) alert("Error: " + status); // status 0 - when load is interrupted
        }
    });
    }

</script>

Your excel.php file should be

<?php
  header('Content-Type: application/vnd.ms-excel');
  header('Content-disposition: attachment; filename='.rand().'.xls');
  echo $_POST["html"];
?>

and if you want to use $_GET then you should refer how can I javascript decodeURI in PHP? to encode and decode uri in javascript to php

Community
  • 1
  • 1
Abhijit Jagtap
  • 2,740
  • 2
  • 29
  • 43