Programmation PHP/Ajax/Date

Un livre de Wikilivres.

Démonstration[modifier | modifier le wikicode]

Le résultat est visible sur http://xavier.merrheim.free.fr/date.

Les fichiers[modifier | modifier le wikicode]

Le fichier index.html[modifier | modifier le wikicode]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Sommaire en PHP !</title>
  <style type="text/css">
  #page1
  {
  position:absolute;
  background-color:#AAAAAA;
  left : 200px;
  width:500px;
  top:10px;
  height:200px;
  }

  #page2
  {
  position:absolute;
  background-color:cyan;
  left:200px;
  width:500px;
  height:200px;
  top:250px;
  }
  </style>

<script type='text/JavaScript'>
var xhr = null; 
var n=0;
function getXhr()
{
     if(window.XMLHttpRequest)xhr = new XMLHttpRequest(); 
else if(window.ActiveXObject)
  { 
  try{
     xhr = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) 
     {
     xhr = new ActiveXObject("Microsoft.XMLHTTP");
     }
  }
else 
  {
  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
  xhr = false; 
  } 
}

function init()
{
loop();
}

function loop()
{
setTimeout('loop();',3000);
ShowPage();
}

function ShowPage()
{
getXhr();
xhr.onreadystatechange = function()
    {
     if(xhr.readyState == 4 && xhr.status == 200)
     {
     document.getElementById('page1').innerHTML=xhr.responseText;
     }
    }

xhr.open("GET","ajax.php",true);
xhr.send(null);
}

</script>

</head>

<body onLoad="init()">

    <div id="page1">
    </div>

    <div id="page2">
        Ce cadre n'est pas mis à jour.<br/>
    <H1>Bienvenue chers amis</h1>
    </div>
  </body>
</html>

Le fichier ajax.php[modifier | modifier le wikicode]

Ce fichier peut contenir tout script php dont le résultat doit être mis à jour régulièrement.

Pour cet exemple :

<?php

echo "(Cadre mise à jour le ".date("r").")";

?>