Pattatharasu Nataraj
Pattatharasu Nataraj

Reputation: 248

Why is my AJAX request in Wordpress not working

I am trying to use an external AJAX page to do something with PHP that inserts into my SQL database. I have tried to import a Wordpress class which is not working.

function accept_request(friend_id, user_id, status){
    $.ajax({
            type:'POST',
            url: myAjax.theme_dir +'callbacks/friend_action.php',
            data:{'friend_id':friend_id, 'user_id':user_id, 'status':status},
            success: function(data){
                alert(data);
                //location.reload();
             }
            });
          }

and the php code is

//require_once($_SERVER['DOCUMENT_ROOT']."/choice/wp-load.php");
require_once("C:/xampp/htdocs/choice/wp-load.php");
global $wpdb; 
$friend_id=$_POST['friend_id'];
$user_id=$_POST['user_id'];
$status=$_POST['status'];


if($status=="D"){
    $wpdb->query("delete from Friends_list where user_id='".$user_id."' and Friend_id='".$friend_id."'");
}
else{
    echo $friend_id.$user_id.$status;
    $wpdb->query("Update Friends_list set status='".$status."' where user_id='".$user_id."' and Friend_id='".$friend_id."'");
}

Upvotes: 0

Views: 60

Answers (1)

Farrukh Subhani
Farrukh Subhani

Reputation: 2038

You need to use a standard guide to implement ajax in wordpress. You need to do following

  1. Make proper ajax call
  2. Create function to handle the request
  3. Add function to hook
  4. Create success/error or other handlers as needed

Upvotes: 1

Related Questions