我使用此代码从wordpress数据库创建一个excel文件。它在localhost上运行得非常好,但是当我在真实的网站上试用它时,它只会打开一个显示标题的新选项卡。我是否缺少一段代码来阻止按钮这样做?
function get_excel()
{
if (isset($_POST['excel'])) {
if (isset($_SESSION['id'])) {
$id = $_SESSION['id'];
}
// echo 'werkt';
global $wpdb, $post;
$file = "orders";
$tblorders= $wpdb->prefix.'b2borders';
$output1="";
$output="";
$column_name = array('voornaam','familienaam','leveradress','land','product','maat','aantal','prijs');
foreach ($column_name as $caname) {
$output .= $caname . "\t";
}
print $output;
$landen = $wpdb->get_results("SELECT DISTINCT land FROM `wp_b2borders` WHERE domainid=$id ORDER BY `wp_b2borders`.`land` ASC");
foreach ($landen as $l)
{
$unewline = "\r\n";
if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'win')) {
$unewline = "\r\n";
} else if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mac')) {
$unewline = "\r";
} else {
$unewline = "\n";
}
$output3 = $unewline.$l->land;
print $output3;
$land = $l->land;
$expid = "SELECT * FROM $tblorders WHERE domainid = $id AND land = '$land' ";
$expids = $wpdb->get_results($expid);
$totprijs=0;
foreach ($expids as $t) {
$vn = $t->voornaam;
$fn = $t->famnaam;
$la = $t->land;
$adress = $t->leveradress;
$product=$t->product;
$aa=$t->aantal;
$maat = $t->maat;
$prijs = $t->prijs;
$pr = $prijs*$aa;
$totprijs +=$pr;
$output1 = $unewline . strip_tags($vn) . "\t" . strip_tags($fn) . "\t" . strip_tags($adress) . "\t". strip_tags($la) . "\t" . strip_tags($product) . "\t" . strip_tags($maat) . "\t". strip_tags($aa) . "\t". strip_tags($pr) . "\t" ;
print $output1;
}
print $unewline.'totaalprijs '.$land.': '.$totprijs;
}
$filename = $file . "_" . date('Y-m-d_H-i', time());
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
// $url = 'admin.php?page=my_pirazzo_edit&id=' . $id;
// wp_redirect($url);
exit;
}
}