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 creating a new plugin,i got this error Fatal error: Call to a member function query() on a non-object in my ajax page. how to solve this error.

this is my code:

if (isset($_REQUEST['ckballvalue'])) 
{    
$allvideos=$_REQUEST['ckballvalue'];
 $now = date("Y-m-d H:i:s", time());
 global $wpdb; 
$insert_video =$wpdb->query("INSERT INTO " . $wpdb->prefix . "hdflvvideoshare (link,post_date)
VALUES ('".$allvideos."','".$now."')") or die('not inserting'); 
}

// i am calling this page by js: 
function chkbox() { 
    $('[name^=checkbox]:checked').each(function() { 
        var ckballvalue=($(this).val()); 
        $.ajax({ 
        type: "POST", 
        url: "<?php echo $site_url; ?>/wp-content/plugins/<?php echo $contus; ?>/ajaxpages/ajaxpage.php", 
        data: "ckballvalue="+ckballvalue, 
        success: function(msg){ } 
    }); 
    }); 
}
share|improve this question
    
Where does this code appear, how are you including it? –  Stephen Harris Mar 15 '12 at 13:21
    
i am calling this page by js: function chkbox() { $('[name^=checkbox]:checked').each(function() { var ckballvalue=($(this).val()); $.ajax({ type: "POST", url: "<?php echo $site_url; ?>/wp-content/plugins/<?php echo $contus; ?>/ajaxpages/ajaxpage.php", data: "ckballvalue="+ckballvalue, success: function(msg){ } }); }); } –  SANS780730 Mar 15 '12 at 13:24

1 Answer 1

up vote 0 down vote accepted

The problem is that you are not loading WordPress, so none of it's included framework is defined (in this particular case the global $wpdb; object and it's methods).

WordPress has built in way of handling ajax requests. In particular you should send the data to the url: admin_url('admin-ajax.php'). Included in the sent data should be the variable action (and which should be unique), i.e. action:'my_action_to_do_something'.

WordPress will then fire one of two hooks:

 'wp_ajax_my_action_to_do_something' //For logged in users
 'wp_ajax_noprov_my_action_to_do_something'//For non-logged in users

You should hook a callback function on to one or both of those hooks (depending if the action should be performed for logged in users, non-logged in users or both). The sent data can be obtained via $_REQUEST.

There are loads of AJAX related questions on this site, so I would recommend reading through some of them. Also, see the Codex pages on ajax in plug-ins.

share|improve this answer
    
i can not understand.please edit my code correctly. –  SANS780730 Mar 15 '12 at 14:03
    
alerdy my js code and ajaxpage code is there,you see above.please edit my code correctly and update me. –  SANS780730 Mar 15 '12 at 14:14
1  
The links, particularly the Codex page, has plenty of examples to get you started, you should take a look at those. –  Stephen Harris Mar 15 '12 at 14:22
    
one more thing:anyone function is not working in my ajaxpage –  SANS780730 Mar 15 '12 at 14:24
    
see error:<br /> <b>Fatal error</b>: Call to undefined function add_action() in <b>C:\xampp\htdocs\wordpress\wp-content\plugins\sankarsplugin\ajaxpages\ajaxpage‌​.php</b> on line <b>125</b><br /> –  SANS780730 Mar 15 '12 at 14:24

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.