需要导出 CSV 时的解决方案,同时在 excel 文件中获取当前页面的 HTML 代码
Need solution for when Exporting CSV, getting the current page's HTML code also in the excel file
这是 Export
按钮的 HTML
代码:
<form method="post" name="download_csv" class="pull-right" enctype="multipart/form-data">
<button type="submit" name="Export" class="btn btn-success" >Export to excel</button>
</form>
这是 Exporting data
到 CSV
的 PHP
代码:
// DB data Export to Excel
if(isset($_POST['Export'])){
header('Content-Type: text/csv; charset= utf-8');
header('Content-Disposition: attachment; filename= data.csv');
$output= fopen("php://output", "w");
fputcsv($output, array('Id','Name','Address', 'Contact no.','Email'));
$query = "Select id,name,address,contact,emailcontact from addresses where user_id='".$_SESSION['id']."' ORDER By id ASC";
$result = mysqli_query($conn, $query);
while($row= mysqli_fetch_assoc($result)){
fputcsv($output, $row);
}
fclose($output);
}
但是当我单击 Export to excel
按钮时,它会导出 correct data
和 current page's
html code
。我不知道为什么会这样。任何解决方案表示赞赏。
因为您的表单操作指向同一个脚本。您应该进行条件检查以避免在同一脚本中出现这种情况,或者简单地更改操作 url.
此问题已通过添加解决:
exit();
在我的 PHP
脚本的末尾。
现在应该是:
`// DB data Export to Excel
if(isset($_POST['Export'])){
header('Content-Type: text/csv; charset= utf-8');
header('Content-Disposition: attachment; filename= data.csv');
$output= fopen("php://output", "w");
fputcsv($output, array('Id','Name','Address', 'Contact no.','Email'));
$query = "Select id,name,address,contact,emailcontact from addresses where user_id='".$_SESSION['id']."' ORDER By id ASC";
$result = mysqli_query($conn, $query);
while($row= mysqli_fetch_assoc($result)){
fputcsv($output, $row);
}
fclose($output);
exit();
}`
这是 Export
按钮的 HTML
代码:
<form method="post" name="download_csv" class="pull-right" enctype="multipart/form-data">
<button type="submit" name="Export" class="btn btn-success" >Export to excel</button>
</form>
这是 Exporting data
到 CSV
的 PHP
代码:
// DB data Export to Excel
if(isset($_POST['Export'])){
header('Content-Type: text/csv; charset= utf-8');
header('Content-Disposition: attachment; filename= data.csv');
$output= fopen("php://output", "w");
fputcsv($output, array('Id','Name','Address', 'Contact no.','Email'));
$query = "Select id,name,address,contact,emailcontact from addresses where user_id='".$_SESSION['id']."' ORDER By id ASC";
$result = mysqli_query($conn, $query);
while($row= mysqli_fetch_assoc($result)){
fputcsv($output, $row);
}
fclose($output);
}
但是当我单击 Export to excel
按钮时,它会导出 correct data
和 current page's
html code
。我不知道为什么会这样。任何解决方案表示赞赏。
因为您的表单操作指向同一个脚本。您应该进行条件检查以避免在同一脚本中出现这种情况,或者简单地更改操作 url.
此问题已通过添加解决:
exit();
在我的 PHP
脚本的末尾。
现在应该是:
`// DB data Export to Excel
if(isset($_POST['Export'])){
header('Content-Type: text/csv; charset= utf-8');
header('Content-Disposition: attachment; filename= data.csv');
$output= fopen("php://output", "w");
fputcsv($output, array('Id','Name','Address', 'Contact no.','Email'));
$query = "Select id,name,address,contact,emailcontact from addresses where user_id='".$_SESSION['id']."' ORDER By id ASC";
$result = mysqli_query($conn, $query);
while($row= mysqli_fetch_assoc($result)){
fputcsv($output, $row);
}
fclose($output);
exit();
}`