View source for ajaxold

Hits :  **{{hits}}**

**OLD-VERSION** 
PLZ visit ((https://balaskas.gr/wiki/ajax ajax))

{{anchor href="top"}}

== Ajax - Php :: Mini How To ==

%%(html5)
Asynchronous JavaScript and XML - PHP Hypertext Preprocessor
%%

{{ToC from="h2" to="h3"}}

https://balaskas.gr/img/ajax/map.png

Η παραπάνω εικόνα είναι αντίγραφό της 
%%http://www.phpbuilder.com/columns/map.png%%

=== Action Example ===

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

https://balaskas.gr/ajax/old_index.php

=== index.php === 

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

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

<script language="JavaScript">
%%
%%(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);
    }
  }
}
%%
%%(html5)
</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)
<?php
$mtime = explode(' ', microtime());
printf('<p align=right>Page loaded in %.4f seconds.', ( ($mtime[0] + $mtime[1]) - $starttime) . "</p>"); 
?>
%%
%%(html5)
</body>
</html>
%%

((#top))
-----------

=== action.php ===

%%(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))
-----------