Ajax with jQuery UI dialog not working

I am trying to update my custom database table when changes are made in a dialog box. The script for the dialog box and ajax call is as follows –

jQuery(document).ready(function($){

$("td > span").click(function(){

    var id = $(this).attr('id');
    var message = "message"+id;
    var content = jQuery("#"+message).text();
    var $dialog = $("<div></div>").html("<textarea style="width:99%; height:90%" class="popup-content">"+content+"</textarea>").dialog({
        height: 400,
        width: 400,
        title: 'My Data',
        modal: true,
        autoOpen: false,
        dialogClass: 'wp-dialog',
        buttons: {
            "Save": function(){
                $("#"+message).html($(".popup-content").val());
                $.ajax({
                    type: "post",
                    url: script_data.admin_ajax,
                    data: {
                        action: "feedmng_update",
                        feed_id: id
                    }
                });
            }
        }
    });
    $dialog.dialog("open");
  });
});

The above script works all well but for the Ajax part. The above code is a separate javascript file which I even enqueue in my php file. The following is the Ajax call related code to update database having the id passed during the Ajax call –

function __construct(){    
  add_action( 'wp_ajax_feedmng_update', array( &$this, 'feedmng_update' ) );
  add_action( 'init', array( &$this, 'feedmng_load_scripts' ) );
}

function feedmng_update(){

    global $wpdb;
    $feedid = $_REQUEST["feed_id"];
    $wpdb->update( $wpdb->feedmanager, array( 'message' => "New data" ), array( 'id',$feedid ) );
}

function feedmng_load_scripts(){

    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'jquery-ui-core' );
    wp_enqueue_script( 'jquery-ui-dialog' );
    wp_enqueue_style (  'wp-jquery-ui-dialog' );
    wp_register_script( 'feedmng-popup', WP_PLUGIN_URL.'/feed-manager/mypopup.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-dialog' ) );
    wp_enqueue_script( 'feedmng-popup' );
    wp_localize_script( 'feedmng-popup', 'script_data', array( 'admin_ajax' => admin_url( 'admin-ajax.php' ) ) );
}

EDIT
Added the following to the script, console shows “Success”

success: function(){
console.log("Success");
}

My table name is wp_feedmanager and I am trying to update it’s ‘message’ column. But it just wont update ? Any suggestions what should be done ?

For future reference –

The problem was here –
The third parameter of update query should be array( ‘id’ => $feedid )
Also the EDIT regarding tablename suggested by Milo needs to be included !

1 Answer
1

Assuming your javascript and ajax action code are otherwise correct, target admin-ajax.php with the correct path by localizing your enqueued script:

wp_localize_script(
    'your_script_handle',
    'script_data',
    array( 'admin_ajax' => admin_url( 'admin-ajax.php' ) )
);

Then in your javascript, reference that URL with:

url: script_data.admin_ajax

EDIT-

ah, missed this the first time: $wpdb->feedmanager isn’t set unless you’ve explicitly set it somewhere. $wpdb->table_name only works for native tables, as those member vars are hard-coded directly into the wpdb class. change it to a string 'wp_feedmanager'.

also note that the wp_ table prefix can be (and should be) changed via wp-config.php, use $wpdb->prefix to make your code more portable:

$table_name = $wpdb->prefix . 'feedmanager';

Leave a Comment