112233
112233

Reputation: 2466

Ajax call not working out in wordpress

I referred to some examples online and modified functions.php and the front end template to fire an ajax call to fetch some data. But I've hard time understanding on hoe the data is returned from the requested url.

At the end of functions.php, I added,

wp_enqueue_script('jquery');
function myFunction(){
    echo "hi";
   die();
}
add_action('wp_ajax_myFunction', 'myFunction');
add_action('wp_ajax_nopriv_myFunction', 'myFunction');

In my custom template page, I added,

var datavalue = 'test data string';

  jQuery.ajax({

      url: "/wp-admin/admin-ajax.php",
      method: "GET",
      data: { 'datavar' : datavalue }

  }).success(function(data) {

      console.log("successfully run ajax request..." + data);

  }).done(function(){

      console.log("I am from done function");

  }).fail(function(){

      console.log("I am from fail function.");

  }).always(function(){

      console.log("I am from always function");

  });

});

After running it, I get these response.

I am from fail function.  
I am from always function

I don't understand how to fetch data from a specific url and display the result in ajax's success function.

I don't even know how the function defined in function.php would be called by this ajax call? How are they related?

Please explain. Also I would like to fire ajax call to query database by passing keyword, how can I do that in wordpress?

Upvotes: 2

Views: 5024

Answers (4)

matthew
matthew

Reputation: 19

In addition to above answers, in your function.php, make use of $_REQUEST. The $_REQUEST contains all the data sent via AJAX from the Javascript call. Something like this

function myFunction() {

    if ( isset($_REQUEST) ) {
{
    global $wpdb;
    $keyword = $_REQUEST["keyword"];
    
    if($keyword){
       
        $query = "
        SELECT `$keyword`, COUNT($keyword) AS Total 
        FROM `profileinfo` GROUP BY `$keyword`
        ";
        $result = $wpdb->get_results($query);
        $data = array();

        foreach($result as $row)
        {
            $data[] = $row
        }
    
        echo json_encode($data);
    }
}
    }
     die();
}
add_action( 'wp_ajax_myFunction', 'myFunction' );
add_action('wp_ajax_nopriv_myFunction', 'myFunction');

Upvotes: 0

Jakir Hossain
Jakir Hossain

Reputation: 2517

You have to use a action on ajax like.

jQuery.ajax({
      url: "/wp-admin/admin-ajax.php",
      method: "GET",
      data: {
            action : 'myFunction'
           'datavar' : datavalue,
       }
  });

PHP function need to edit.

function myFunction(){
    echo 'success calling functions';
    wp_die();
}

Upvotes: 1

Chava Geldzahler
Chava Geldzahler

Reputation: 3730

Your AJAX function should include an action parameter to tell admin-ajax which function you would like to execute.

  url: "/wp-admin/admin-ajax.php",
  method: "GET",
  data: {
        action : 'myFunction'
  }

(or, if you are set up for it, then you can include it in your url, as below)

url: "/wp-admin/admin-ajax.php?action=myFunction"

Also, your function in functions.php should be written in PHP:

function myFunction(){
    echo 'hello';
    die();
}

Upvotes: 1

user7912022
user7912022

Reputation:

you are not passing the "action" parameter in "data". Which contains callback function's name. Please check the attached link. https://www.sitepoint.com/how-to-use-ajax-in-wordpress-a-real-world-example/

In this you've to make a callback functions. Wordpress dsnt work with the specific url.

But if you still want to use the specific url. Follow the steps:- 1. Make a wordpress template. 2. Add your specific url code there. 3. Make a page in the admin panel and assign the template you've created above. 4. Now the permalink of that page can be used as a specific url in the jQuery ajax.

Upvotes: 0

Related Questions