Hits : 3021


OLD-VERSION
PLZ visit ajax


Ajax – Php :: Mini How To 


Asynchronous JavaScript and XML – PHP Hypertext Preprocessor



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

http://www.phpbuilder.com/columns/map.png

Action Example


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


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

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