1

all

In my theme there is a notification at top. After user login he can see the notification update. when click on notification i want to make change in database alert field value 0 to 1.

My ajax call in theme

 $("#notifications-button").click(function() {

        $.ajax({
        type : 'POST',
        url: "http://localhost/rupp/wp-admin/admin-ajax.php",
        data:{action:'MyAjaxFunction'},
        success : function(data){
            $('.message-count').hide();
        },

        });


});

and in function.php file

add_action('wp_ajax_MyAjaxFunction','MyAjaxFunction');
function MyAjaxFunction()
{
    global $wpdb;
  echo (int)$user_ID;
  $wpdb->update( $wpdb->wp_frm_items, 
            array( 'alerts' =>  1)
            );
}

but it not make any changes in alert fields in db. is that right way to call theme function using ajax?

2
  • Does your $wpdb->update() method work when you're not calling it via ajax? Mar 6, 2012 at 10:22
  • No that is the issue .. i tried with just echo also but its not replaying me anything in consol
    – Ajay Patel
    Mar 6, 2012 at 10:26

1 Answer 1

2

You need to add the $where parameter to your update statement because it doesn't know which row to update. The $wpdb->update() method has no default value for the $where argument so you must supply one.

http://codex.wordpress.org/Class_Reference/wpdb#UPDATE_rows

Try this:

$wpdb->update( $wpdb->wp_frm_items, 
        array( 'alerts' =>  1),
        array( 'user' => get_current_user_id() )
        );

Your Answer

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

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