导出到PHP中的html表,按钮单击事件不起作用

问题描述 投票:0回答:1

我正在制作一个应用程序,其中数据是从我的sql以html表格式生成的。我想以excel格式生成这些数据。这是我的代码:

demo.php

<?php   
 include 'include/db_connection.php';   
 $query = "select * from main_master";  
 $result = mysql_query($query);  
?> 

 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | Export HTML table to Excel File using Jquery with PHP</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
      </head>  
      <body>  
           <br />  
           <div class="container" style="width:700px;">  
                <h3 class="text-center">Export HTML table to Excel File using Jquery with PHP</h3><br />  
                <div class="table-responsive" id="employee_table">  
                     <table class="table table-bordered">  
                          <tr>  
                               <th width="10%">Id</th>  
                               <th width="30%">Name</th>  
                               <th width="10%">Gender</th>  
                               <th width="50%">Designation</th>  
                          </tr>  
                          <?php   
                          while($row = mysql_fetch_assoc($result))  
                          {  
                          ?>  
                          <tr>  
                               <td><?php echo $row['B']; ?></td>  
                               <td><?php echo $row['D']; ?></td>  
                               <td><?php echo $row['E']; ?></td>  
                               <td><?php echo $row['F']; ?></td>  
                          </tr>  
                          <?php                           
                          }  
                          ?>  
                     </table>  
                </div>  
                <div align="center">  
                     <button name="create_excel" id="create_excel" class="btn btn-success">Create Excel File</button>  
                </div>  
           </div>  
           <br />  
      </body>  
 </html>  
 <script>  
 $(document).ready(function(){  
      $('#create_excel').click(function(){  
           var excel_data = $('#employee_table').html();  
           var page = "excel.php?data=" + excel_data;  
           window.location = page;  
      });  
 });  
 </script>  

excel.php

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

在执行上面的代码时,什么也没做。 excel没有生成。请帮忙。

javascript php jquery html
1个回答
0
投票

我想一切都运行正常,只是你试图在网址中发送太多数据

 $('#create_excel').click(function(){  
           var excel_data = $('#employee_table').html();  
           var page = "excel.php?data=" + excel_data;  
           window.location = page;  
 }); 

您无法在get request (Query string)中发送完整的html表格

只需将用户重定向到excel.php页面,如果需要创建sql query,则在查询字符串中发送必需参数。

然后而不是echo $_GET["data"];

demo.php上写下excel.php的完整代码

现在excel.php看起来像

<?php   
 header('Content-Type: application/vnd.ms-excel');  
 header('Content-disposition: attachment; filename='.rand().'.xls');  
 include 'include/db_connection.php';   
 $query = "select * from main_master";  
 $result = mysql_query($query);  
?> 

 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | Export HTML table to Excel File using Jquery with PHP</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
      </head>  
      <body>  
           <br />  
           <div class="container" style="width:700px;">  
                <h3 class="text-center">Export HTML table to Excel File using Jquery with PHP</h3><br />  
                <div class="table-responsive" id="employee_table">  
                     <table class="table table-bordered">  
                          <tr>  
                               <th width="10%">Id</th>  
                               <th width="30%">Name</th>  
                               <th width="10%">Gender</th>  
                               <th width="50%">Designation</th>  
                          </tr>  
                          <?php   
                          while($row = mysql_fetch_assoc($result))  
                          {  
                          ?>  
                          <tr>  
                               <td><?php echo $row['B']; ?></td>  
                               <td><?php echo $row['D']; ?></td>  
                               <td><?php echo $row['E']; ?></td>  
                               <td><?php echo $row['F']; ?></td>  
                          </tr>  
                          <?php                           
                          }  
                          ?>  
                     </table>  
                </div> 
           </div>  
           <br />  
      </body>  
 </html>

你可以从中删除cssjs文件。

您还需要更新脚本

 <script>  
 $(document).ready(function(){  
      $('#create_excel').click(function(){  
           var page = "excel.php";  
           window.location = page;  
      });  
 });  
 </script>  

或者你可以尝试这个lib(Datatable):https://datatables.net/extensions/buttons/examples/initialisation/export.html

© www.soinside.com 2019 - 2024. All rights reserved.