我正在为自己的需要制作一个csv导出器插件。但我的问题是,当我单击“导出”按钮时,它会将整个页面下载为html。这是我的密码。请帮我解决这个问题。这个脚本可以作为php应用程序完美地工作。提前感谢!
<?php
/*
CSV Exporter
*/
add_action(\'admin_menu\',\'jsrmsp_csv_export_menu\');
add_action(\'admin_init\',\'jsrmsp_csv_export_main\');
/* Csv Export Menu */
function jsrmsp_csv_export_menu(){
add_submenu_page( \'edit.php?post_type=jsrmsp_sr\', \'Export CSV\', \'Export CSV\', \'manage_options\', \'jsrmsp_csv_export_page\', \'jsrmsp_csv_export_admin_page\' );
}
function jsrmsp_csv_export_main(){
if( isset( $_POST[\'csves\'] ) ){
/* CSV Exporter function */
// output headers so that the file is downloaded rather than displayed
//header(\'Content-Type: text/csv; charset=utf-8\');
header(\'Content-type: application/csv\');
//header(\'Cache-Control: no-cache, must-revalidate\');
//header(\'Pragma: no-cache\');
//header(\'Expires: 0\');
header(\'Content-Disposition: attachment; filename=data.csv\');
// create a file pointer connected to the output stream
$output = fopen(\'php://output\', \'w\');
// output the column headings
fputcsv($output, array(\'Name\', \'Profession\', \'Phone\'));
// fetch the data
mysql_connect(\'localhost\', \'root\', \'\');
mysql_select_db(\'csv-exporter\');
$rows = mysql_query(\'SELECT name,profession,phone FROM records\');
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
}
}
function jsrmsp_csv_export_admin_page() { ?>
<div class="wrap">
<!-- Some inline style -->
<style type="text/css">
.jsrmsp-m-title {
margin: 0 !important;
padding: 8px 12px !important;
}
.csvcol {
float: left;
width: 25%;
}
</style>
<?php
// Check form is submited
if( isset( $_POST[\'csves\'] ) ){
jsrmsp_csv_export_main();
?>
<div id="jsrmsp-imload" class="updated fade">
<p><img style="height: 15px; vertical-align: middle;" src="<?php echo plugin_dir_url(__FILE__); ?>../assets/images/loader.gif" alt="" />Please wait...</p>
</div>
<?php }
?>
</div><!-- end wrap -->
<div class="wrap">
<h2><?php _e(\'Export CSV\'); ?></h2>
<!-- Ajax Response Message -->
<div class="ajax-response-message">
</div>
<div id="dashboard-widgets-wrap">
<div class="metabox-holder" id="dashboard-widgets">
<div id="postbox-container" class="postbox-container">
<div id="side-sortables" class="meta-box-sortables ui-sortable"><div style="display: block;" id="dashboard_quick_press" class="postbox ">
<div class="handlediv"><br></div><h3 class="hndle jsrmsp-m-title" style="cursor: default;"><span><span class="hide-if-no-js">Export Csv File</span></span></h3>
<div style="overflow: hidden;" class="inside">
<form id="jsrmsp-export" class="jsrmsp-import" action="" method="post">
<div style="margin: 10px 0; overflow: hidden;" class="input-text-wrap">
<div class="csvcol">
<?php
// Exam Class term
$terms = get_terms(\'classes\');
if(!empty($terms) &&!is_wp_error($terms)) { ?>
<select name="class" id="class" required>
<option value=""><?php _e(\'Select Exam\',\'jsrmsp-td\'); ?></option>\';
<?php
foreach($terms as $term) {
echo \'<option value="\'.$term->name.\'">\'.$term->name.\'</option>\';
}
?>
</select>
<?php }
?>
</div>
<div class="csvcol">
<?php
// Exam Year Term
$terms = get_terms(\'years\');
if(!empty($terms) &&!is_wp_error($terms)) { ?>
<select name="year" id="year" required>
<option value=""><?php _e(\'Select Year\',\'jsrmsp-td\'); ?></option>
<?php
foreach($terms as $term) {
echo \'<option value="\'.$term->name.\'">\'.$term->name.\'</option>\';
}
?>
</select>
<?php }
?>
</div>
<div class="csvcol">
<?php
// Exam Section Term
$terms = get_terms(\'sections\');
if(!empty($terms) &&!is_wp_error($terms)) { ?>
<select name="section" id="section">
<option value=""><?php _e(\'Select Section\',\'jsrmsp-td\'); ?></option>
<?php
foreach($terms as $term) {
echo \'<option value="\'.$term->name.\'">\'.$term->name.\'</option>\';
}
?>
</select>
<?php }
?>
</div>
<div class="csvcol">
<?php
// Exam Groups Term
$terms = get_terms(\'groups\');
if(!empty($terms) &&!is_wp_error($terms)) { ?>
<select name="group" id="group">
<option value=""><?php _e(\'Select Group\',\'jsrmsp-td\'); ?></option>
<?php
foreach($terms as $term) {
echo \'<option value="\'.$term->name.\'">\'.$term->name.\'</option>\';
}
?>
</select>
<?php }
?>
</div>
</div>
<p class="submit">
<input name="csves" id="csves" class="button button-primary" value="Export" type="submit">
</p>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php }
?>