detect changes in database without reloading the page - php

I create a chat webpage.
by ajax I send data to database but for showing new data to both user in chat I have to detect last changes in database.
for this problem (detect last changes without reloading page) I create a loop for each 5 sec to connect to the data base and if there was a change, return that.
but i think its not functional.
any body knows what is the correct way to solve this?

Related

Laravel: Displaying data from db after clicking a button - but staying on the same page

That's the task I am currently facing:
Starting point: I have a form, which accepts data and saves it into the database.
What needs to be done: I need to include a button called "show data" (a different one than the "submit" button in the form, it can even be a clickable div), which will then display the data from the database on the same page, under the form. The button should also enable refreshing the list after new data was inserted into the form.
Now, if the data from the database had to be displayed on a different page, that could be handled easily with routing. However, for this particular task, I need to display the data on the same page.
My question: Is there a pure Laravel/PHP way to solve that problem? I am considering using AJAX (to which I am also new), but if there is some Laravel specific solution then I would prefer that.
The solution needs to work on localhost.
For PHP, without having any help from JavaScript, showing any changes to the database will require a browser refresh. On the front-end, you can use VueJS and Axios to help you with these requests, which do not require the browser to be refreshed.

how to get the the data instantly after inserting in database in php?

I am creating a website audit tool now in this someone will insert details of their website and answer some questions and i am inserting that in the database. Now I want to show results in new page according to the choices user makes now how can I get the details of that inserted data instantly any way to get that through PHP?
Yes, the same way you would normally get data. When loading the new page, have your controller load the data and then include it.
If this is asynchronous or doesn't include a page refresh, then you could handle this more quickly with JavaScript.

Echo new row when it got added to database

I am trying for a few days now to figure out how I can do this.
I have two pages, one is index.php, other is add.php .
1)Index.php - I want it do display last 5 rows from database table.
2)Add.php - contains form which adds new row to database.
How can I update values that are shown on index.php when I add new row?
So lets say I have index.php already opened on one tab, and on other one is Add.php. When I fill the form on add.php,
I want it to automatically notifies index.php that there is a new row,and that it should change values without refreshing the index.php tab.
Do I need VPS for this?What is the best way to do this theoretically?
There is no need in setting up a VPS for this. A VPS is Just a server in which u can install your own software. You are already using a server for PHP (local perhaps).
U can create a websocket server. Once you add a row you send a message tot the websocket server which will then tell the index page to resfresh.
Previous option is a lot of work for a rather simple functionaliteit. I'd suggest the simplest option as recommended by FrankerZ. Only if you refresh the page at a set interval using JavaScript (window.setInterval) then the page would keep refreshing. I don' t think that's ideal. You van however send a xhtmlhttprequest to store the contents for the index page and run that request every x seconds. Then u can compare the 2 contents and decide if the page should be refreshed or not.

What is the best way to update the webpage only when a new entry is saved in the database?

I am wondering what will be the best way to update the webpage only when a new entry is saved in the database?
For example, suppose one of my visitor is reading a post on my blog. At the same time some other visitor comments on that particular blog post. Then I want that latest comment to appear on that page without reloading the page.
I know the technology used here will be AJAX/jQuery but will I have to run the AJAX continuously every time using something setTimeout()?
If I do so, then it will generate a lot of http request endlessly. Is it possible that AJAX runs only when it is needed indeed and not all the time?

View live updates from MySQL database?

I want to create a page where people can insert some text, hit enter, and the text be stored in a MySQL database. I can do this, but I want to be able to load a page, enter a password, and see a list of all the info in said database, then whenever something is added to the database, it's added to the list on the page, without me needing to refresh the page or setup some javascript code to refresh the page every five seconds.
I believe Satya has it correct in suggesting that you use Ajax in order to refresh the data without refreshing the page. You can have it request updated information from a php script which queries your database for the data you wish to display, and then sets the elements on your page accordingly.
this is probably the best way for you to implement ajax calls using javascript
http://api.jquery.com/jQuery.ajax/
Or you an simly do it with the help of setInterval() function. You can call an html code in a div using
$('#id').html("<htmlcode></htmlcode>");
Example : http://jsfiddle.net/ipsjolly/995PJ/6/

Categories