Hi, I have a script that generates a html table when you input some data. I want to export it to excel. I have an excel code but when I run it on the browser a warning message appears. here's my code:
<?php
include ("database_con.php");
echo '<form action="generated.php" method="post">';
echo "Company:"." ".'<input type = "text" name="ccode">';
echo " ";
echo "Select Month"." ".'<select name="month">';
echo "<option>"." "."</option>";
echo '<option value="01">'."January"."</option>";
echo '<option value="02">'."February"."</option>";
echo '<option value="03">'."March"."</option>";
echo '<option value="04">'."April"."</option>";
echo '<option value="05">'."May"."</option>";
echo '<option value="06">'."June"."</option>";
echo '<option value="07">'."July"."</option>";
echo '<option value="08">'."August"."</option>";
echo '<option value="09">'."September"."</option>";
echo '<option value="10">'."October"."</option>";
echo '<option value="11">'."November"."</option>";
echo '<option value="12">'."December"."</option>";
echo "</select>";
echo " ";
echo "Select Year:"." ".'<select name="year">';
echo "<option>"." "."</option>";
echo '<option value="2010">'."2010"."</option>";
echo '<option value="2011">'."2011"."</option>";
echo '<option value="2012">'."2012"."</option>";
echo '<option value="2013">'."2013"."</option>";
echo '<option value="2014">'."2014"."</option>";
echo '<option value="2015">'."2015"."</option>";
echo '<option value="2016">'."2016"."</option>";
echo '<option value="2017">'."2017"."</option>";
echo '<option value="2018">'."2018"."</option>";
echo '<option value="2019">'."2019"."</option>";
echo '<option value="2020">'."2020"."</option>";
echo '<option value="2021">'."2021"."</option>";
echo "</select>";
echo " ";
echo '<input type="submit" name="generate" value="GENERATE">';
echo "<br />";
if(isset($_POST["generate"]))
{
$coy_code = $_POST["ccode"];
$month = $_POST["month"];
$year = $_POST["year"];
$trans_query = mysql_query ("Select ccode,rno, tcode, tdate, id, quantity, itemno, iprice From stk_trs_d where code = '$ccode' and `tdate` like '%$year-$month-%'");
$filename = "company_data_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$table='<table border="1" name="reports">';
echo $table;
echo "<tr>";
echo "<th>Company</th>";
echo "<th>Number</th>";
echo "<th>Transaction</th>";
echo "<th>Transaction Date</th>";
echo "<th>Code</th>";
echo "<th>Quantity</th>";
echo "<th>Item Number</th>";
echo "<th>Item Price</th>";
echo "</tr>";
while($reports = mysql_fetch_array( $trans_query))
{
echo "<tr>";
echo "<td>" . $reports['ccode'] . "</td>";
echo "<td>" . $reports['rno'] . "</td>";
echo "<td>" . $reports['tcode'] . "</td>";
echo "<td>" . $reports['tdate'] . "</td>";
echo "<td>" . $reports['id'] . "</td>";
echo "<td>" . $reports['quantity'] . "</td>";
echo "<td>" . $reports['itemno'] . "</td>";
echo "<td>" . $reports['iprice'] . "</td>";
echo "</tr>";
}
}
echo "</form>";
mysql_close($con);
?>
and here is the warnings:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\company_reports\generated.php:6) in C:\xampp\htdocs\company_reports\generated.php on line 56
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\company_reports\generated.php:6) in C:\xampp\htdocs\company_reports\generated.php on line 57
is there something wrong with my code?? also, the save dialog box doesn't appear every time I press the submit button instead the warning appers. I will wait for your ideas. Thanks