Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
jQuery is a fast, concise, JavaScript Library that simplifies how you traverse HTML
documents, handle events, perform animations, and add Ajax interactions to your web
pages. jQuery is designed to change the way that you write JavaScript.
## chat.sql
```sql
CREATE TABLE IF NOT EXISTS `chat` (
`Id` int(11) NOT NULL auto_increment,
`Text` text NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
```
## server.php
```php
<?
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'your password';
$dbname = 'chat_database';
mysql_select_db($dbname);
$message = $_POST['message'];
if($message != "")
{
$sql = "INSERT INTO `chat` VALUES('','$message')";
mysql_query($sql);
}
while($row = mysql_fetch_array($result))
echo $row['Text']."\n";
?>
```
Ok this script is very simple.
```php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'your password';
$dbname = 'chat_database';
mysql_select_db($dbname);
?>
```
```php
$message = $_POST['message'];
```
```php
if($message != "")
{
$sql = "INSERT INTO `chat` VALUES('','$message')";
mysql_query($sql);
}
```
```php
$sql = "SELECT `Text` FROM `chat` ORDER BY `Id` DESC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
echo $row['Text']."\n";
```
## index.php
```html
<html>
<head>
<script type="text/javascript" src="jquery-1.2.6.pack.js"></script>
<script type="text/javascript">
function update()
{
$.post("server.php", {}, function(data){ $("#screen").val(data);});
setTimeout('update()', 1000);
}
$(document).ready(
function()
{
update();
$("#button").click(
function()
{
$.post("server.php",
{ message: $("#message").val()},
function(data){
$("#screen").val(data);
$("#message").val("");
}
);
}
);
});
</script>
</head>
<body>
</body>
</html>
```
```html
<script type="text/javascript" src="jquery-1.2.6.pack.js"></script>
```
Then we define a new function.
It's used to open server.php (the PHP script I described just above) and copy all
content from it to a textbox with id = screen. And as you can see above content of
server.php will be list of all messages.
```php
function update()
{
$.post("server.php", {}, function(data){ $("#screen").val(data);});
setTimeout('update()', 1000);
}
```
In this part we use $.post method, which is described well here. But I will try to
explain it myself anyway.
```js
function(data)
{
$("#screen").val(data)
;}
```
We want our script to check server for new messages every second.
We will make an recursive function, each time it runs it will call itself but after
1 second, so it will execute itself every second.
`setTimeout('update()', 1000); `
`$(document).ready(function() `
Then we call update() function once and it will keep calling itself.
`update();`
Now when element with id = button is clicked, we must send new message to server
and update our "screen" (a textbox with id = screen).
This part is very similar to update() function except, in this case we send one
variable with POST method, and it is content of new message which is inside the
input box with id = message.
```js
$("#button").click(
function()
{
$.post("server.php",
{message: $("#message").val()},
function(data){
$("#screen").val(data);
$("#message").val("");
}
);
}
);
```
And that's it! Now just add textarea, input box and a button with right id-s!
```html