Is there a method to receive real-time status updates using AJAX jQuery?

I'm attempting to retrieve a status from the database using jQuery AJAX. There are 3 potential options for the status: if it is pending, I want to continue loading the request; however, if the status is changed to success or error in the database, the loading request should be interrupted and return the new status (either error or success).

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!

Answer №1

Essentially, the function checkStatusRealTime is invoked every 5 seconds to execute an AJAX call that retrieves the current 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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing Jquery for JSON Data Parsing

I have some JSON data that looks like this: {"product":["{productTitle=ABCD , productImage=/abcd.jpg, productPath=CDEF.html, productPrice=$299}","{productTitle=EFGH, productImage=xxxx.jpg, productPath=ggfft.html, productPrice=$299}"]} In my JSP page, I&a ...

The contrast between FormData and jQuery's serialize() method: Exploring the distinctions

Recently I came across a situation where I needed to submit a form using AJAX. While researching the most efficient method, I discovered two popular approaches - some developers were utilizing jQuery#serialize() while others were opting for FormData. Here ...

Executing PHP code on button click in HTMLThe process of running a PHP script when

I am currently working on a project that involves detecting facial expressions using Python. However, I need to pass an image to this code through PHP. The PHP code provided below saves the image in a directory. How can I trigger this code using an HTML ...

Looking for assistance with JQuery and JavaScript?

I oversee a team of employees, each with a 7-day work schedule. To streamline the process, I have developed a PHP form that I would like to use to collect data for verification in JavaScript before submitting it to an SQL database using AJAX. My main cha ...

php for the upcoming week and the following week

I have successfully retrieved the date of the Monday for the current week, but I am unsure how to display it along with the date for the following week's Monday in a sentence. Can someone assist me with this issue? For instance, I would like the outp ...

Analyzing the path of the cursor

Looking to enhance my tracking capabilities by monitoring mouse movements. I have the ability to capture XY coordinates, but understand that they may vary depending on browser size. Are there any other parameters recommended for accurate results? P.S: Be ...

Access denied in /bin/node even after changing permissions to 777

This pertains to Amazon EC2 running on Linux. I have a PHP script that triggers a shell script execution. Within the shell script, there is a command to execute node. Executing the PHP script from the command line successfully runs the node command. Ho ...

What is the process of fetching the selector value through AJAX and storing it in PHP?

I am working with the prop() method in jQuery and I need to extract the value "show_pdf1" in PHP, save it for processing before returning the final result from readPdf.php. How can I achieve this using only the property method without any additional method ...

Guide on making a personalized object in JavaScript

I am struggling with a piece of JavaScript code that looks like this: var myData=[]; $.getJSON( path_url , function(data){ var len = data.rows.length; for (var i = 0; i < len; i++){ var code = data.rows[i].codeid; var ...

Hover over an element to trigger the background fading effect on the parent div

I'm looking to create a hover effect on an element within a fullscreen div. When I hover over the element, I want a background image to fade in on the div with the class ".section." While I can easily display the background image, I am unsure of how t ...

prettyPhoto popup exceeds maximum width and height limitations

I am currently using the most up-to-date version from No Margin for Errors and I have set allow_resize to true. However, the size of the display is still too large. Is there a way to set a maximum width/height? I have already configured the viewport as fo ...

Using jQuery to pass apostrophes from PHP into an input field

Currently, I am working with database information that includes a character of blah^s (I have replaced the ' character with ^ in order to locate apostrophes within the row). I am using preg_replace to add apostrophes back into the string. Everything i ...

Ajax request (with Spring) causing HTTP error 415Charsets deems invalid

My server doesn't receive a response when I send an Ajax request. PUT request: function modifyData() { var item={ id:idNum.replace('edit',''), data:newData }; console.log(item); ...

PHP - session expires upon page refresh

I'm in the process of creating a login system for my website and I've run into an issue with updating the navigation bar once a user has logged in. Every time I refresh the page, it seems like the session gets lost and the navigation bar doesn&ap ...

Please send the element that is specifically activated by the onClick function

This is the HTML code I am working with: <li class="custom-bottom-list"> <a onClick="upvote(this)"><i class="fa fa-thumbs-o-up"></i><span>upvote</span></a> </li> Here is my JavaScript function for Upvot ...

Use regular expressions to extract information enclosed within quotation marks

Here is the string we have: feature name="osp" We want to extract specific parts of this string and create a new string. The word "feature" may vary, as well as the content inside the quotes, so our solution needs to be flexible enough to capture any var ...

Sending Data to Backend Using React Router and Express with an Ajax POST Request

I've encountered an issue while attempting to submit a basic form within a React component: class UploadPartList extends Component { constructor(props) { super(props); this.state = { data: [] }; this.handleSubmit = this.handleSubmit.bi ...

Error in JSON access permissions while using AJAX call to Webmethod in ASP.NET webforms with jquery Datatables

My current project involves implementing server-side processing for jQuery Datatables.NET in an ASP.NET webforms test application. Here is the code I am using: $(document).ready(start); function start(){ $('#PersonsTable').DataTable({ ...

Enhancing HTML "range" element with mouse scroll functionality for incrementing values in step increments

I'm working on developing a scroll feature that operates independently from the main window's scrolling function. I aim to trigger specific events in the primary window based on interactions with this separate scrollbar. The only solution I coul ...

What is the best way to incorporate an if else condition using the <?php if($loggedin): ?> statement within JavaScript code to display a button push or pop response from the server side?

I would like to verify this php if condition code ''<?php if($loggedin) : ?>'' inside JavaScript code in order to display one of the buttons, either push or pop. I want to keep this button hidden from the client side by embedding ...