php - Issue with EXPORT to CSV FILE -
i want export write in input text :
-i have created class called "excelfile", contain methods exporting data csv file.
-i have issue, when try export file data, colone added automatically in csv file, colone contain code html of table
-this code :
excelfile.php
<?php class excelfile { private $csv = null; function colonne($file) { $this->csv.=$file."\n"; return $this->csv; } function insertion($file){ $this->csv.=$file."\n"; return $this->csv; } function output($nomfichier){ header("content-type: application/vnd.ms-excel"); header("content-disposition: attachment; filename=$nomfichier.csv"); print $this->csv; exit; } } ?>
index.php
<?php include_once 'excelfile.php'; ?> <html> <form method='post' action="index.php"> <table> <tr> <td><label>first name</label></td> <td><input type='text' name='tfirst' /></td> </tr> <tr> <td><label>last name</label></td> <td><input type='text' name='tlast' /></td> </tr> <tr> <td><input type='submit' name='btnsave' value='export csv file' /></td> </tr> </table> </form> </html> <?php if (isset ( $_post ["btnsave"] )) { test (); } function test() { $excelfile = new excelfile (); $excelfile->colonne ( "first name;last name" ); $excelfile->insertion ( $_post ['tfirst'] . ';' . $_post ['tlast'] ); $excelfile->output ( 'export result' ); } ?>
your output() function called after have sent whole html document, why included in file.
you need move code around bit index.php file looks more this:
<?php include_once 'excelfile.php'; if (isset ( $_post ["btnsave"] )) { test (); exit; //you want end here, html below not in csv } function test() { $excelfile = new excelfile (); $excelfile->colonne ( "first name;last name" ); $excelfile->insertion ( $_post ['tfirst'] . ';' . $_post ['tlast'] ); $excelfile->output ( 'export result' ); } ?> <html> <form method='post' action="index.php"> <table> <tr> <td><label>first name</label></td> <td><input type='text' name='tfirst' /></td> </tr> <tr> <td><label>last name</label></td> <td><input type='text' name='tlast' /></td> </tr> <tr> <td><input type='submit' name='btnsave' value='export csv file' /></td> </tr> </table> </form> </html>
(side note: please consider using php framework)
(side note: please consider using commas separate values instead of semi-colons)
Comments
Post a Comment