ebalaskas.gr: ajax ...

Home Page | Index | Changes | Comments | Users | Registration | Σύνδεση  Συνθηματικό:  
 

Hits : 426


Ajax – Php :: Mini How To 


Asynchronous JavaScript and XML - PHP Hypertext Preprocessor


Περιεχόμενα


ajax (15 Kb)


Η παραπάνω εικόνα είναι αντίγραφό της 

Action Example


Εάν θέλετε να δείτε ένα παράδειγμα λειτουργίας βασισμένο στην αρχιτεκτονική ajax, πατήστε εδώ:


http://ebalaskas.gr/ajax/

index.php


<?php
$starttime 
explode(' 'microtime());
$starttime $starttime[1] + $starttime[0];

?>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Names</title>

<script language="JavaScript">

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject() {
  var xmlHttp;
  if(window.ActiveXObject) {
    try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
       xmlHttp = false;
    }
  }
  else {
    try {
       xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
       xmlHttp = false;
    }
  }
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else
    return xmlHttp;
}

function process() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    name = encodeURIComponent(document.getElementById("myName").value);
    xmlHttp.open("GET", "action.php?name=" + name, true);
    xmlHttp.onreadystatechange = handleServerResponse;
    xmlHttp.send(null);
  }
  else
    setTimeout('process()', 1000);
}

function handleServerResponse() {
  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
//document.getElementById("divMessage").innerHTML = "1 " + xmlHttp.responseXML.documentElement.firstChild.data;
document.getElementById("divMessage").innerHTML = xmlHttp.responseXML.getElementsByTagName("final")[0].firstChild.data;

      setTimeout('process()', 1000);
    }
    else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}
</script>

 </head>

<body onload='process()'>

<p align="center">
  <input type="text" id="myName" />
</p>

<p align="center">
  <b><div id="divMessage" /></div></b>
</p>

<p align="center"><small>james - john - michael - david</small></p>

<p align="center"><a href="<?php echo $PHP_SELF?>">Reload</a></p>

<?php
$mtime 
explode(' 'microtime());
printf('<p align=right>Page loaded in %.4f seconds.', ( ($mtime[0] + $mtime[1]) - $starttime) . "</p>"); 
?>
</body>
</html>


top


action.php


<?php
header
('Content-Type: text/xml');
echo 
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
if ( !empty ( 
$_GET['name'] ) ) {

$array = array('john''james''michael''david');
$name array_search($_GET['name'], $array);
if (!empty(
$name)) $name "Hello Master " $_GET['name'];
else 
$name "I dont know you";

}else 
$name="Give name";
echo 
"<final>".$name."</final>";
?>


top



 
Χρόνος: 0.046 s
Μνήμη: 1.931 Mb

Valid XHTML 1.0 Transitional