0

I am New to Wordpress.I am creating a Plugin Where I have to import a csv File to database.Uploaded and Entered data Successfully.But my Problem is once the file gets uploaded it shows a blank page with the success Message.How can I redirect to the same Page with success or error Message please help..Thanks in Advance.. Here is my code

$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 Answer 1

1

You can remove the else statement so that user will see the message and will be able to upload csv again or you can redirect using

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

Edit

$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' );

In the redirected page you can display message from the $_SESSION['pd_services_total_import'] an then unset( $_SESSION['pd_services_total_import'] )

6
  • Thanks for your response @sabarnix, I have doubt here Is there any problem in submitting a form in the same page?
    – John
    Jul 17, 2014 at 7:39
  • I have never faced any issue, I have done it many times
    – sabarnix
    Jul 17, 2014 at 7:42
  • Thanks @sabarnix.Will it makes a duplitae row if the user press f5?
    – John
    Jul 17, 2014 at 7:52
  • Yes it will. In order to avoid that you you can either use the above code or do the for submit checking in wp hook and redirect using wp_safe_redirect()
    – sabarnix
    Jul 17, 2014 at 7:59
  • can you suggest me any examples please @sabarnix
    – John
    Jul 17, 2014 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.