user715564 user715564 - 1 year ago 61
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() {
type: "GET",
url: my_ajax.ajaxurl,
data: {
'action': 'dallas_db_facebook_make_post'
beforeSend: function() {
success: function(data){


error: function(){

Answer Source

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'];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download