skip to Main Content

I am trying to call a php function with a button press.

This is my code so far:

    echo("<button onclick="import_file()"> Import </button>");        

The Javascript upload.js file:

    function import_file () {
   
     jQuery.ajax({
        url: my_ajax_object.ajax_url, 
        data: {action: 'final_save'},
        type: "POST", //request type
        success:function(result){
         alert(result);
       }
     });
 }

and the action in the primary php file:

 function final_save(){
    check_admin_referer('import-upload');
    set_time_limit(0);
    $this->file = $file;
    $result = $this->save();
    if ( is_wp_error( $result ) ){
        echo $result->get_error_message();
    }else{
        echo "Success";
    }

}

add_action('wp_ajax_final_save', 'final_save');
add_action('wp_ajax_no_priv_final_save', 'final_save');

I also enqueued the scripts in the same file:

function enqueue_script(){
    $plugins_url = plugins_url();
    $base_url = get_option( 'https://www.virtualmin.archives.com' );
    $plugins_dir = str_replace( $base_url, ABSPATH, $plugins_url );
    $plugin = $plugins_dir.'/really-simple-csv-importer/upload.js';
    wp_enqueue_script('js-file', $plugin, array('jquery'), '', false);
    wp_localize_script( 'js-file', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

    //Load jQuery
    wp_enqueue_script('jquery');

}

and calling this function from dispatch:

    function dispatch() {
        $this->header();
        $this->enqueue_script();
//more code
}

Dispatch being called here:

    function really_simple_csv_importer() {
    load_plugin_textdomain( 'really-simple-csv-importer', false, dirname( plugin_basename(__FILE__) ) . '/languages' );
    
    $rs_csv_importer = new RS_CSV_Importer();
    register_importer('csv', __('CSV', 'really-simple-csv-importer'), __('Import posts, categories, tags, custom fields from simple csv file.', 'really-simple-csv-importer'), array ($rs_csv_importer, 'dispatch'));
}
add_action( 'plugins_loaded', 'really_simple_csv_importer' );

I am getting an admin-ajax.php 400 (Bad Request) error. Any idea what am I doing wrong? This is being coded in a plugin.

3

Answers


  1. I seem to remember reading that you need to register, localize and then enqueue the script when using ajax so instead of

    wp_enqueue_script('js-file', $plugin, array('jquery'), '', false);
    wp_localize_script( 'js-file', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
    

    try

    wp_register_script('js-file', $plugin, array('jquery'), '', false);
    wp_localize_script( 'js-file', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
    wp_enqueue_script('js-file', $plugin, array('jquery'), '', false);
    

    I hope this helps

    Also I know this is probably not the problem, but I thought you needed to have wp_die() or die; always in the function being called ie final_save

    Login or Signup to reply.
  2. I can see that there is typo error in your php file so just replace following line

    add_action('wp_ajax_no_priv_final_save', 'final_save');
    

    with following one

    add_action('wp_ajax_nopriv_final_save', 'final_save');
    

    Hope this help to solve error.

    Login or Signup to reply.
  3. Besides the answers already given by @jtowell, you’re gonna want to add a nonce to your localization and check the referer on the backend. As this is might be confusing the first time around I highly recommend following the steps in the documentation.

    General walkthrough:
    https://developer.wordpress.org/plugins/javascript/ajax/

    Specific docs:
    https://developer.wordpress.org/reference/functions/check_ajax_referer/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search