Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 !

share|improve this question
    
You have a type in your $wpdn->feedmanager. It should be $wpdb->feedmanager. –  josh Nov 10 '13 at 15:55
    
Ok, I corrected that too...that aint a problem. Still not working. –  Navin Nov 10 '13 at 15:58
    
Are you receiving a success in your console when you run the ajax? –  josh Nov 10 '13 at 15:59
1  
url: "admin-ajax.php" is almost certainly not the right path to admin-ajax.php –  Milo Nov 10 '13 at 16:03
    
@josh Receiving no success –  Navin Nov 10 '13 at 16:18

1 Answer 1

up vote 1 down vote accepted

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';
share|improve this answer
    
Tried what you suggested...no success yet ! –  Navin Nov 10 '13 at 16:37
    
edit your question and add your current code, including your script enqueue code. use your browser's error console to check for errors and/or 404s. –  Milo Nov 10 '13 at 16:39
    
Added more relevant code –  Navin Nov 10 '13 at 17:47
    
see edit above. –  Milo Nov 10 '13 at 18:23
1  
you'll have to do some more in-depth debugging then- hardcode an update that's not within an ajax request and get that working, echo every var at every stage to make sure it's what you expect, get a simple ajax request without the update code working, etc.. –  Milo Nov 10 '13 at 18:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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