I am creating a mobile app that uses a MySQL database and a PHP API. I need to update the client UI when the MySQL database is updated. I have been told that the way to this is by using periodic AJAX requests. After doing some research, it looks like AJAX requests are related to jquery. Can I create periodic AJAX requests using only PHP code?
-
1You can make an HTTP request from any platform you want. – SLaks Aug 15 '17 at 21:25
-
Aren't AJAX and HTTP requests different? – Mr. Man Aug 15 '17 at 21:26
-
You know the difference between client and server, right? – u_mulder Aug 15 '17 at 21:27
-
@Mr.Man: No; the term "AJAX" means sending an HTTP request from Javascript in the browser. – SLaks Aug 15 '17 at 21:27
-
The requests are currently coming from the client, the PHP API handles the requests (99% sure) – Mr. Man Aug 15 '17 at 21:29
-
AJAX requests are client side JavaScript requests. Read up on it here - https://www.w3schools.com/xml/ajax_intro.asp – OwChallie Aug 15 '17 at 21:29
-
the J in aJax stands for javascript. – coderodour Aug 15 '17 at 21:29
-
So if I am writing my mobile app in swift, how can I keep it updated when changes occur in the database? – Mr. Man Aug 15 '17 at 21:29
-
[How to make an HTTP request in Swift?](https://stackoverflow.com/questions/24016142/how-to-make-an-http-request-in-swift) – coderodour Aug 15 '17 at 21:33
-
coderodour I am already using Alamofire to make HTTP requests in my Swift App. Once the request has been made, does it stay updated? What if the data in the MySQL database changes after the HTTP request has already been made? – Mr. Man Aug 15 '17 at 21:35
3 Answers
AJAX is in fact an XMLHttpRequest, that is simply put, an asynchronous HTTP request.
JQuery can handle most of the coding for you using the jquery.ajax function.
If you want to do ajax calls (or just plain HTTP requests) using php you need to check PHP Cron jobs.
Instead of using ajax in a cron job, you can use PHP Curl to perform the HTTP request as you are working server side. Ajax calls are done from the client side only.
Putting parts of those togheter you'll end up where you want to be.

- 1,610
- 1
- 19
- 25
AJAX stands for Asynchronous JavaScript and XML. While it is by no means limited to sending XML data, it is something that's done through JavaScript.
When your browser loads a page, it sends a request to the URL, downloads the content of that page, then follows embedded content (like images, JS, and CSS content) to include in the page. Once all of the content is limited, the document is finished, and browser steps loading content.
AJAX is a mechanism by which you can make calls to the server after the page has finished loaded. You can send data to the server, request data from the server, and manipulate the page content (without reloading it) with the data that you receive.
For your specific question, you'd like want to look at the setInterval()
function to loop through a function to periodically make a call to a server-side script (which can be PHP), which will return the updated data, and then use JavaScript on the client side to modify the page based on the updated content received.

- 3,682
- 1
- 19
- 21
-
Is there a way to send a request when the database is updated, rather than sending requests based on a time interval? – Mr. Man Aug 15 '17 at 21:40
-
Not with AJAX. AJAX is initiated from the client, so the server can only send data to the client when the client requests it. Instead of AJAX, what you want is WebSockets. That way, when your app loads, it will maintain a persistent connection with the server, then the server can shoot out updates to the client as they occur without the client having to periodically ask for them. – Nick Coons Aug 15 '17 at 21:42
-
Any idea how to use WebSockets with swift and iOS? If you know how, could you point me in the right direction? – Mr. Man Aug 15 '17 at 21:52
-
No, I'm not familiar with coding for either of those. But I imagine Google would be your friend for something like that. – Nick Coons Aug 15 '17 at 22:10
By what I understand from your question and comments, you have a mobile client, written in Swift (so I'm assuming it's an iOS app?), and a server that runs with PHP. As it has been previously said, no, you can't update the client from the PHP code: you need to have some kind of communication between the client and server. There are three main choices here:
- Use polling. This involves making an HTTP request from your client from time to time (say, like every minute? or every two minutes?). In this way, your client app would be constantly asking the server if anything has changed, and if it has, then it would update the UI. The disadvantage would be that when there is a change to your database, your UI wouldn't be updated until your next HTTP call, so, if you put your calls every 5 minutes, your client would have to wait at most 5 minutes to be updated. And if your put less time between your calls, you will generate a lot of traffic (which is not desirable, specially in a mobile client which may generate data charges) and possibly overload your server with useless requests.
- Use long polling. This is a "smart" way of making polling, which allows you to update your interface as soon as there is a change, and at the same time reduce the amount of calls made to the server. There are many questions on long polling on Stack Overflow.
- Use push notifications. This allows you to send a notification directly to the user's phone, even if your application is not running. But they require a lot of security and configuration. There is a good tutorial on push notifications for iOS here.
And no, in this case the term "AJAX" doesn't apply, since AJAX refers to server requests made from Javascript in web pages, not in mobile applications written in Swift ;)

- 1,347
- 1
- 13
- 26