admin管理员组文章数量:1130349
I am making a csv exporter plugin for my own need. But the problem I have is that, it download whole page as html when I click Export button. Here is my code. Please help me to slove this problem. This script work perfectly as a php application. Thanks in advance!
<?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 }
?>
I am making a csv exporter plugin for my own need. But the problem I have is that, it download whole page as html when I click Export button. Here is my code. Please help me to slove this problem. This script work perfectly as a php application. Thanks in advance!
<?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 }
?>
Share
Improve this question
asked Mar 6, 2017 at 11:59
S.k.joyS.k.joy
231 silver badge7 bronze badges
1 Answer
Reset to default 3Seems like you do not die after you generate the CSV which meas that wordpress will continue to generate whatever is the relevant admin HTML after it had generated the CSV.
I am making a csv exporter plugin for my own need. But the problem I have is that, it download whole page as html when I click Export button. Here is my code. Please help me to slove this problem. This script work perfectly as a php application. Thanks in advance!
<?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 }
?>
I am making a csv exporter plugin for my own need. But the problem I have is that, it download whole page as html when I click Export button. Here is my code. Please help me to slove this problem. This script work perfectly as a php application. Thanks in advance!
<?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 }
?>
Share
Improve this question
asked Mar 6, 2017 at 11:59
S.k.joyS.k.joy
231 silver badge7 bronze badges
1 Answer
Reset to default 3Seems like you do not die after you generate the CSV which meas that wordpress will continue to generate whatever is the relevant admin HTML after it had generated the CSV.
本文标签: phpDownload full html page with CSV export plugin
版权声明:本文标题:php - Download full html page with CSV export plugin 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749142959a2322517.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论