Reputation: 1102
I need to be able to have a PHP page that can allow one person to send a POST request to it with information and then every user who already has the page open will automatically be updated to reflect the new information in the POST request without refreshing the page.
I have a bit of experience with PHP and I can handle receiving a POST request and reading/writing to a database, I just need to know how (if possible) I can have this PHP script send this new information to all connected clients without requiring them to refresh the page.
Thanks for any insight!
Upvotes: 2
Views: 3077
Reputation: 2329
Ok Aaron, I have to go on here because there is no much space in the comment to explain this.
If you are already using ajax
to post the data to the database
then you can do it so:
in the page you need to be refreshed add an other ajax request
in order to retrieve the posted values from the database and wrap it in a function, as example:
function get_post(){
var Url = 'here the url that points to your php script'
$.ajax({
url: Url,
type: 'POST',
dataType: 'json',
data: { //if needed you need to pass here the parameters },
})
.done(function(data) {
//here assign the returned data to the html element
})
.fail(function() {
console.log("error");
});
}
after that you will need this in order to call the function on periodical intervals:
setInterval(function(){
get_post()
},5000);
This will execute the above mentioned funcion every 5 seconds (adjust this value following your needs) The page will refresh without reloading.
NOTE: I don't know what you are storing and what you are going to show, so depending of what you are planning to do you will need to add some additional code in the returned response and maybe you will need to pre-format your outputted html
directly in your php
script so you can display it in your page in one go.
I hope this will give you an idea on how to do it.
Upvotes: 6