下载带有CSV导出插件的完整html页面

时间:2017-03-06 作者:S.k.joy

我正在为自己的需要制作一个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 }
?>

1 个回复
最合适的回答,由SO网友:Mark Kaplun 整理而成

你好像不知道die 生成CSV后,意味着wordpress在生成CSV后将继续生成相关的管理HTML。

相关推荐

如何将Java脚本添加到Custom-Page.php模板?

如何将javascript添加到自定义页面。php模板?如何使从w3schools ajax教程获得的以下javascript在自定义页面上工作。php模板?任何帮助都将不胜感激。工作javascript包含在以下HTML中:<!DOCTYPE html> <html> <style> table,th,td { border : 1px solid black; border-collapse: collapse;&#x