Been trying to figure this out since a hell of a lot of hours, but without result. A user can select some filters in a form, which result in a unique table, printed to the screen.
Then I want a button to export this specific table to a csv file, stored on the users computer.
if(isset($_POST["askReport"])){
//all the variables
//...
$myQuery = ""SELECT *, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdagU) - pauzetijdU AS totaalurenU, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdag) - pauzetijd AS totaaluren FROM `newRegister` $u $aP $pD $uF $sD"";
$result = $mysqli->query($myQuery);
if($result->num_rows >0){
print '<table><tr><th>.....'; //and all the headers
print '<td>'.$row["user"].'</td>';
..... //and all the other columns and end of row
}
So far, so good, I get a nice table with the results that I want. But then comes the tricky part - how do I get the button to download this table? Here's what I tried: (this code is stil in the isset($_POST["askReport"]) if statement)
// DOWNLOAD TABEL
print '<form method="POST"><button name=download>download</button></form>';
if(isset($_POST["download"])){
$select = "SELECT *, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdagU) - pauzetijdU AS totaalurenU, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdag) - pauzetijd AS totaaluren FROM `newRegister` $u $aP $pD $uF $sD";
$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $export , $i ) . "\t";
}
while( $row = mysql_fetch_row( $export ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "
";
}
$data = str_replace( "" , "" , $data );
if ( $data == "" )
{
$data = "
(0) Records Found!
";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=download .xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header
$data";
};
}
This doesn't give any errors. It just reloads the page. So I can't figure out why it isn't working. I thank you for your help.