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'm trying to send datas via jquery ajax to my plugins class to update a filed in a table and I'm getting the following error

Fatal error: Call to a member function query()

my class looks like

class MyClass{

    function __construct(){

        global $wpdb;
        $this->db = $wpdb;
        //add_filter('query_vars', 'parameter_queryvars' );
    }

//Save ajax datas
    public function save_tips($token, $tips){
            $this->db->query(
                $this->db->prepare(
                    "UPDATE wp_competitors SET results= %s, update_time= %d",
                    $tips ,$cur_date = date('Y-m-d H:i:s'),  "WHERE token = $token
                ") // $wpdb->prepare
            ); 

    }
}

}

when I send the datas with ajax the entry file is my plugin main file and I fire on with the following code

if ($_POST['results']){
    $token = '393a9276ae329e00b3739d2e76e52f3b';
    $tips = json_encode($_POST['results']);
    $save = new Tipspiel();
    $save->save_tips($token, $tips);

    return 'OK';
}

it seems like all wordpress funtions are ignored

updating the codes after the feedback from @toscho

I was adding the following in my javascript

$j("form#tiepspiel").submit(function(e) {
        e.preventDefault(); // this disables the submit button so the user stays on the page            
        var str = $j(this).serialize();
       //alert(decodeURIComponent(str));
        if ($j('ul#sortables li').size() > 10){

            $j( "#dialog-modal" ).dialog({
                    height: 140,
                    modal: true
                });
            return;
        }
        var data = {
                action: 'TIPPS',
                     tips: str,
                    _ajax_nonce: "<?php echo wp_create_nonce( 'my_ajax_nonce' ); ?>"

            };

        $j.post(ajaxurl, data, function(response) {
            alert('Got this from the server: ' + response);
            //$j.cookie("tipspiel", "competitor", { expires: 1 });
           $j('form#tiepspiel').fadeOut('slow', function() {
                // Animation complete.
              });
        });              
    }); 

and adding this in my plugins entry file

add_action('wp_ajax_nopriv_TIPPS', 'tipps_processing_function');



function tipps_processing_function(){
    check_ajax_referer('my_ajax_nonce');

    if ($_POST['results']){
        $token = '393a9276ae329e00b3739d2e76e52f3b';
        $tips = json_encode($_POST['results']);
        $save = new Tipspiel();
        $save->save_tips($token, $tips);

        return 'OK';
    }
    //do stuff here
}

is not clear how to pass the recived variables to my class

share|improve this question
add comment

1 Answer

This cannot work. Read What's the preferred method of writing AJAX-enabled plugins?

Do not try to access you plugin files directly.

share|improve this answer
    
thank you for the feedback I have been trying out and couple of things are not quite clear for me. The jquery seems to work but the function is not getting called. If I update the changed codes could you give me some ideas how to handle? Thanks in advanced –  fefe Dec 2 '12 at 14:32
add comment

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.