I am looking to learn about AJAX and I have what I think is a simple problem that can teach me. Suppose I have a table in a database into which new entries are being added by users. I want to construct a web page that will display the latest n entries, as new entries are added, without refreshing the entire page. Where should I start with th开发者_运维技巧is problem?
Take a look at jquery
it'S very easy with jquery and it would look like
$("#your_div").load('new_entries.php');
//new_entries.php
include("db_connect.php");
$result = mysql_query("SELECT * FROM new_entries");
while($row = mysql_fetch_assoc($result)){
echo $row['id']." has new value: ".$row['value'];
}
table would look like
CREATE TABLE `new_entries` (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`value` varchar(99) NOT NULL
);
Basically AJAX works like this. Your user updates a form on the html page, and clicks submit or the changes are detected. A javascript function is called which opens a server connection, and sends the updated data to a php or server file, which updates the database and reloads the new data and returns it. This same javascript function then waits for a reply from the server, which has sent back the new data. The javascript function can then update the html, with the new data from the server. No page refresh.
So you need an AJAX javascript function and some server side code to handle the database update.
The jQuery library has prebuilt AJAX functions, which is why so many use it.this
精彩评论