0

Instead of reporting the data from the database and showing it in excel , it shows in excel what's on the screen .. I would like it to show what's contained in the $excel variable .. below is my code

if(isset($_POST['excel_report'])){

    $select_query = "SELECT * FROM question_data";
    $result = mysql_query($select_query);
    $excel ='';
    $excel .= "<table border ='0'>";
    $excel .= "<tr> <td> Question Title </td> <td> Question Answer </td> </tr>";
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

        $excel .="<tr> <td>".$row['question_title']."</td> <td>".$row['question_response']."</tr>";

    }

    $excel .="</table>";
    $filename ="questiondata.xls";
    header('Content-type: application/ms-excel');
    header('Content-Disposition: attachment; filename='.$filename);
    echo $excel;

}

my HTML form , for the form , the elements are dynamically added with jquery .. I'm not using any of the form data though , I am just using the submit button for excel to get the data from the database and display it on a spreadsheet .. where did I go wrong ?

<form action="/cms/form_test.php" method="post">

        <h1> Test Form </h1>

        <div class="test">
            <input type ="hidden" name ="form_id" value ="4" />
            <div id ="select_data">
                <select id="select"> </select> <br /><br />

            </div>
            <div id ="options"></div>
            <div id ="select_state">
                <div id ="state_title"> </div>
                <select id="select_state_dropdown"> </select> <br /><br />

            </div>
        </div>
        <input type="submit" name ="excel_report" value="Export Question Data to Excel" />
        <input type="submit" name ="submit" value="submit form" />
    </form>
Stack Overflow
  • 247
  • 2
  • 5
  • 14
  • Hm. It seems you don't understand file formats. It is not enough to set header to excel. You should make the $excel to contains exel format data – bksi Oct 25 '13 at 20:29
  • how does that work ? It doesn't display the content of $excel , instead it displays the HTML already on the screen .. – Stack Overflow Oct 25 '13 at 20:33
  • You have to build excel data. There are few php libraries that could do that Example: http://stackoverflow.com/questions/10595599/which-is-the-best-way-to-generate-excel-output-in-php – bksi Oct 25 '13 at 20:34
  • This is nothing to do with PHPExcel at the moment, despite being tagged as such; but you could actually use the PHPExcel library from [github](https://github.com/PHPOffice/PHPExcel) or [codeplex](https://phpexcel.codeplex.com/) to generate a real BIFF (.xls) or OfficeOpenXML (.xlsx) file – Mark Baker Oct 26 '13 at 20:04

0 Answers0