-3

Please help... am working on a form within as html page that export my SQL table to excel and download it. but i have been experience error

Anythime i click the Export to excel button it keep taking me to a new page instead of export data to excel. Below is my form and the excel.php script

Form

<form method="post" action="sites/erp/ajaxify/excel.php">
    <div id="content">
        <div class="col-md-4 col-md-offset-4 text-center">
                <div class="form-group">
                    <button id="exportbtn" class="btn btn-lg btn-success btn-block">  Export to Excel </button>
                </div>  
        </div>
    </div>
</form>

and the Excel.php

if(isset($_REQUEST['tablename']) && isset($_REQUEST['keyy'])){
$tablename = use_if_sent('tablename');
$result = $ez_db->query("SELECT `firstname`, 'lastname', 'email', 'gender', 'user_group', 'phone', status' FROM 'signup'");
 if(mysql_num_rows($result) > 0)
{
        $output .= '
        <table class="table" bordered="1">  
            <tr>  
                <th>First name</th>
                <th>Lastname</th>
                <th>Email</th>
                <th>Gender</th>
                <th>Department</th>
                <th>Phone</th>
                <th>Status</th>
            </tr>
        ';
        while($row = mysql_fetch_array($result))
        {
           $output .= '
            <tr>  
                <td>'.$row["firstname".'</td>  
                <td>'.$row["lastname"].'</td>  
                <td>'.$row["email"].'</td>  
                <td>'.$row["gender"].'</td>  
                <td>'.$row["user_group"].'</td>
                <td>'.$row["phone"].'</td>
                <td>'.$row["status"].'</td>
            </tr>
           ';
        }
      $output .= '</table>';
      header('Content-Type: application/xls');
      header('Content-Disposition: attachment; filename=download.xls');
      echo $output;
    }  
}
?>
VinothRaja
  • 1,405
  • 10
  • 21

2 Answers2

-1

Please correct it this code:

<td>'.$row["firstname".'</td> 

To this :

<td>'.$row["firstname"].'</td>
TrickStar
  • 229
  • 4
  • 19
-1

In Excel.php

<td>'.$row["firstname".'</td> 

should change as

<td>'.$row["firstname"].'</td> 

You missed to close the square brace ].

And you are not doing excel export here. You are setting Content Type to application\xls to html content. This will not export excel.

See Excel export tutorial to get an idea of excel export.

Change your html code to this.

    <form method="post" action="sites/erp/ajaxify/excel.php" target="exportExcelFrame">
    <div id="content">
        <div class="col-md-4 col-md-offset-4 text-center">
                <div class="form-group">
                    <button id="exportbtn" class="btn btn-lg btn-success btn-block">  Export to Excel </button>
                </div>  
        </div>
    </div>
</form>
<iframe style="display:none;" id="exportExcelFrame" name="exportExcelFrame"></iframe>