Home > front end >  There is any way to get status change in real time using AJAX jQuery?
There is any way to get status change in real time using AJAX jQuery?

Time:01-13

I'm trying to get a status from database using jQuery AJAX. For this status I have 3 options, if the status is pending I want to keep loading the request, if I go to database and change this status to success or error, the loading request will interrupt and return the status(error and success status).

jQuery Code

$.ajax({
type:'get',
url:getStatus.php?record_id=50,
success: function(e){
  console.log(e)
} 
});

PHP Code - getStatus.php

<?php 

require_once (class/class.php');

$stat = new Stat();

$record_id= $_GET['record_id'];

$status = $stat->getStatus($record_id);

echo $status;

PHP Code - geStatus() Class Method

  public function getStatus($record_id){
     
    $query = "  SELECT `status` from records "; 
    $query.= " WHERE `record_id`='{$record_id}' ";

    /* Get query response */
    $response = $this->QueryKey($query);

    
    if($response==true){

        $output['success']=$response[0];
    }
    
    return json_encode($response);
}

Thank you!

CodePudding user response:

Simply, You call ajax code in every 5 sec duration and get the status...

function checkStatusRealTime(){
    $.ajax({
        type:'get',
        url:getStatus.php?record_id=50,
        success: function(e){
            if(status == 'pending'){
                // code...
            }else if(status == 'success'){
                clearInterval(realTimeCheck);
            }else{

            }
        } 
    });
}
let realTimeCheck = setInterval(checkStatusRealTime,5000); //call every 5sec
  • Related