如何在提交表单后重定向到页面

时间:2014-07-17 作者:John

我是Wordpress的新手。我正在创建一个插件,我必须在其中导入一个csv文件到数据库。已成功上载并输入数据。但我的问题是,一旦文件上传,它就会显示一个空白页面,并显示成功消息。如何重定向到带有成功或错误消息的同一页请帮助。。提前感谢。。这是我的密码

$admin_services =   add_menu_page(\'Fee Schedule\',\'Fee Schedule\',\'administrator\',\'pd-services\',\'pd_services\');
<?php
function pd_services(){
if ( $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST[\'admin_service_csv_upload\']))          {
        if(get_file_extension($_FILES["service_upload_csv"]["name"])!= \'csv\')
        {
            $error[] = \'Only CSV files accepted!\';
        }
    if (!$error){
        $tot = 0;
        $handle = fopen($_FILES["service_upload_csv"]["tmp_name"], "r");
            //echo $file   =  $_FILES["service_upload_csv"]["tmp_name"];
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                for ($c=0; $c < 1; $c++) {
                    //only run if the first column if not equal to firstname
                    if($data[0] !=\'Department Name\'){
                        $insert_csv_query = "INSERT INTO wp_services(
                                    dept_name,
                                    inc_10_codes,
                                    description,
                                    uc_fee
                                                )VALUES(
                                    \'".mysql_real_escape_string($data[0])."\',
                                    \'".mysql_real_escape_string($data[1])."\',
                                    \'".mysql_real_escape_string($data[2])."\',
                                    \'".mysql_real_escape_string($data[3])."\'
                                    )";
                            $insert_csv_query;
                        $inserted = $wpdb->query($insert_csv_query);
                        }

                    echo $tot++;
            }
    }
fclose($handle);
$content.= "<div class=\'success\' id=\'message\'> CSV File Imported, $tot records added         </div>";
}
} else {
?>
<div class="wrap">
    <form name="csvupload" action="" method="POST" enctype="multipart/form-data">
        <table class="form-table">
            <tr>
                <th><label for="uploadcsv">Upload Services from CSV</label></th>
                <td><input type = "file" name="service_upload_csv" id="upload_csv_id" aria-required="true"></td>
            </tr>
            <td>
            <p class="submit">
                <input type="submit" class="button-primary" name="admin_service_csv_upload" value="<?php _e(\'Save Changes\')?>" />
            </p>
        </td>
        </table>
    </form>
    <?php echo $content ?>
</div>
<?php
}
}
?>

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

您可以删除else语句,以便用户可以看到消息并能够再次上载csv,或者您可以使用

echo \'<script>windows.location.href="\' . $url . \'"</script>\'; die;

编辑

$admin_services = add_menu_page( \'Fee Schedule\', \'Fee Schedule\', \'administrator\', \'pd-  services\', \'pd_services\' );
function pd_services() {
?>
<div class="wrap">
    <form name="csvupload" action="" method="POST" enctype="multipart/form-data">
        <table class="form-table">
            <tr>
                <th><label for="uploadcsv">Upload Services from CSV</label></th>
                <td><input type="file" name="service_upload_csv" id="upload_csv_id" aria-required="true"></td>
            </tr>
            <td>
                <p class="submit">
                    <input type="submit" class="button-primary" name="admin_service_csv_upload"
                           value="<?php _e( \'Save Changes\' ) ?>"/>
                </p>
            </td>
        </table>
    </form>
    <?php echo $content ?>
</div>
function process_pd_services_upload() {
if ( $_SERVER["REQUEST_METHOD"] == "POST" && isset( $_POST[\'admin_service_csv_upload\'] ) ) {
    if ( get_file_extension( $_FILES["service_upload_csv"]["name"] ) != \'csv\' ) {
        $error[] = \'Only CSV files accepted!\';
    }
    if ( ! $error ) {
        $tot    = 0;
        $handle = fopen( $_FILES["service_upload_csv"]["tmp_name"], "r" );
        //echo $file   =  $_FILES["service_upload_csv"]["tmp_name"];
        while ( ( $data = fgetcsv( $handle, 1000, "," ) ) !== false ) {
            for ( $c = 0; $c < 1; $c ++ ) {
                //only run if the first column if not equal to firstname
                if ( $data[0] != \'Department Name\' ) {
                    $insert_csv_query = "INSERT INTO wp_services(
                                dept_name,
                                inc_10_codes,
                                description,
                                uc_fee
                                            )VALUES(
                                \'" . mysql_real_escape_string( $data[0] ) . "\',
                                \'" . mysql_real_escape_string( $data[1] ) . "\',
                                \'" . mysql_real_escape_string( $data[2] ) . "\',
                                \'" . mysql_real_escape_string( $data[3] ) . "\'
                                )";
                    $insert_csv_query;
                    $inserted = $wpdb->query( $insert_csv_query );
                }

                echo $tot ++;
            }
        }
        fclose( $handle );
        $_SESSION[\'pd_services_total_import\'] = $tot;
        wp_safe_redirect( admin_url( \'your admin url here\' ) );
    }
 }
}
add_action( \'wp\', \'process_pd_services_upload\' );
在重定向页面中,您可以显示来自$_SESSION[\'pd_services_total_import\'] 那么unset( $_SESSION[\'pd_services_total_import\'] )

结束

相关推荐

在构建自定义查询时使用包含upgrade.php

我开始构建一个插件,其中需要使用wpdb 对象这是我第一次使用它。所以我查看了如何使用它。我看了一些例子,有一件事让我很困惑:upgrade.php实际上,当我创建一个自定义查询时,只有当我包含upgrade.php 这样做:require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' ); $custom_posts = $wpdb->get_results( \'SELECT * FROM \' . self::TABLE . \'\