user715564 user715564 - 4 months ago 8
Javascript Question

How to use jQuery to get variable from PHP

I am trying to get data from MySQL using ajax and jQuery. Right now, the only thing being returned is "0". Here is my PHP function:

function dallas_db_facebook_make_post () {
global $wpdb;
$query = "SELECT * FROM wp_dallas_facebook WHERE time > NOW() ORDER BY time LIMIT 1";
$results = $wpdb->get_results($query, ARRAY_A);

foreach($results as $result) {
$fbpost = $result['text'];
}

}


Here is my jQuery function:

function send_fb_post() {
jQuery.ajax({
type: "GET",
url: my_ajax.ajaxurl,
data: {
'action': 'dallas_db_facebook_make_post'
},
beforeSend: function() {
},
success: function(data){

console.log(data);

},
error: function(){
},
});
};

Answer

You are not getting anything, because you are not printing/echoing anything.

What happens if you try

foreach($results as $result) {
    echo $fbpost =  $result['text'];
}