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

6
  • 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(). Commented Sep 12, 2016 at 7:08
  • @Paul i have seen the tutorial,which was working for them webslesson.blogspot.in/2016/02/… Commented Sep 12, 2016 at 7:09
  • 1
    you might need to url encode the excel_data Commented Sep 12, 2016 at 7:13
  • what is in console.log(excel_data); ? Commented Sep 12, 2016 at 7:14
  • @Abhijit,it is something like this excel.php?data= <table border="2" width="100%"> ..... whole data Commented Sep 12, 2016 at 7:17

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

,how come you are so accurate,i have removed Activity on C# Corner (%) and is working man
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.