Quantcast
Channel: Topic Tag: MySQL | WordPress.org
Viewing all articles
Browse latest Browse all 5540

ianhaycox on "Secondary php plugin file isn't connected to the database"

$
0
0

I think you'll need a handler in your plugin to manage the request.

E.g.

add_action('wp_ajax_myplugin_getdata', 'myplugin_getdata');

or

add_action('wp_ajax_myplugin_getdata', array($plugin,'getdata'));

if your plugin is in a class with $plugin as your plugin instance.

In ajax.js you need to execute a GET with an action parameter matching the action above minus the wp_ajax_ bit,

e.g.

$.ajax({
    type:"GET",
    url: http://myblog/wp-admin/admin-ajax.php,
    data:"action=myplugin_getdata&"+data,
    success: function(returned_data) {
        $('#display').html(returned_data);
    }
....

then in myplugin_getdata retrieve your GET parameters, perform a database lookup and issue a

die('My returned data');

You can get the AJAX url via admin_url( 'admin-ajax.php' ) and pass it to the ajax.js after calling wp_enqueue_script via wp_localize_script.

For non-logged in users you'll also need a,

add_action('wp_ajax_nopriv_myplugin_getdata', 'myplugin_getdata');

Ian.


Viewing all articles
Browse latest Browse all 5540

Trending Articles