1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| <?php
/************************************************/// Fonction Actu
function afficher()
{
echo "1 ";
try {$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');}
catch(Exception $e) {die('Erreur : '.$e->getMessage()); }
$rep=new xajaxResponse();
$chat = "<table>";
echo "1 ";
$reponse = $bdd->query('SELECT * FROM chat');
// On affiche chaque entrée une à une
while ($donnees = $reponse->fetch())
$chat .= ("<tr><td>" . $donnees['name']. " : </td><td> " . $donnees['msg'] . "</td></tr>");
$reponse->closeCursor(); // Termine le traitement de la requête
echo $chat . "</table>";
$rep->assign('divchat', 'innerHTML', $chat);
return $reponse;
}
function envoyer($posteur, $message)
{
$reponse = new xajaxResponse();
try {$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');}
catch(Exception $e) { die('Erreur : '.$e->getMessage());}
// Insertion du message à l'aide d'une requête préparée
$req = $bdd->prepare('INSERT INTO chat (name, msg) VALUES(?,?)');
$req->execute(array($posteur, $message));
$reponse->clear('message', 'value');
$reponse->call('xajax_afficher');
return $reponse;
}
/************************************************/// Lancemenet AJAX
require_once('./xajax_core/xajax.inc.php');
$xajax = new xajax();
$xajax->register(XAJAX_FUNCTION, 'afficher');
$xajax->register(XAJAX_FUNCTION, 'envoyer');
$xajax->processRequest();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- <link href="style.css" media="all" rel="stylesheet" type="text/css" /> -->
<SCRIPT LANGUAGE="Javascript" SRC="js/jquery.js"> </SCRIPT>
<script type="text/javascript">
function refresh()// Code javascript qui va appeler la fonction afficher toutes les 5 secondes.
{
xajax_afficher();
setTimeout(refresh, 5000);
}
</script>
<?php $xajax->printJavascript(); ?>
</head>
<body >
<div id="divchat" style="margin:auto; height:300px; width:50%; overflow:auto">
<?php afficher();?></div>
<form action="">
<fieldset>
<legend>Entrer ici votre message :</legend>
<div style="margin:auto">
<label>Nom : <input type="text" size="15" id="posteur" /></label>
<label>Message : <input type="text" size="50" id="message" /></label>
<input type="submit" value="Envoyer" onclick="xajax_envoyer(document.getElementById('posteur').value, document.getElementById('message').value); return false;" />
</div>
</fieldset>
</form>
<script type="text/javascript">
refresh();//On appelle la fonction refresh() pour lancer le script.
</script>
</body>
</html> |
Partager