LYF_HKN LYF_HKN - 9 months ago 39
Javascript Question

Is there a better way to update user view instead of refreshing?

In the database I have something like Line, Word and Character. That is, a Word must belong to a specific Line, the same goes for Character (or you can think of a multi-level menu, each menu item can be another menu). I'm writing a management page to interact with the database. Each time the user insert/delete a record, the page must be refreshed to show the changes. However, this approach is too annoying for my users (although the refresh is automatic). Is it possible to show the changes without refreshing the whole page? If it is, how? Thank you.

UPDATE: The data is shown as multi-level list (the picture below is an example) and can be collapsed/expanded. When I refresh the page, they are all collapsed (which annoys my users).


You can do it using AJAX(Asynchronous JavaScript And XML). You can update a web page without reloading it using ajax. You can use jQuery ajax() which is so simple to integrate.

Here is an example-

       method: "get",
       url: "/action.php",
       data: {item_id: item_id, vote: vote},
       dataType: "html"
       // Update view of the web page, it could be deleting or updating a div

method - get/post
url - which will handle your request
data - data you want to send to server
dataType - response type, html/json/text etc

If it works without any problem then the done method will trigger otherwise fail method will trigger.