$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Download full html page with CSV export plugin|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Download full html page with CSV export plugin

matteradmin9PV0评论

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
Add a comment  | 

1 Answer 1

Reset to default 3

Seems 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.

Post a comment

comment list (0)

  1. No comments so far