2016年2月12日 星期五

ajax 的基本例子

index.php  內容

大概流程就是   

First name: <input type="text" onkeyup="showHint(this.value)">
                           |
                           v
              function showHint(str) 
                           |
                           v
        xmlhttp.open("GET", "gethint.php?q=" + str, true);
                           |
                           v
   document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                           |
                           v
   <p>Suggestions: <span id="txtHint"></span></p>     


文字筆記如下:

<p>Suggestions: <span id="txtHint"></span></p>  -> 


他內嵌了一段 javascript....   以 <script> 開頭...
                                </script> 結尾....
裡面的實作是一個 function  showHint(str)

先判斷  str.length 的數值...如果為零...就直接返回空字串....

如果不為零.... 首先取得  xmlhttp = XMLHttpRequest()...

然後check xmlhttp.onreaystatechange ....

如果 xmlhttp.readyState == 4 而且  xmlhttp.status == 200
     
則 document.getElementById("txtHint").innerHTML = xmlhttp.responseText

"txtHint" 為 html 主體裡面的   <p>Suggestions: <span id="txtHint"></span></p>

接下來執行

xmlhttp.open("GET", "gethint.php?q=" + str, true);
xmlhttp.send();

他會去執行  gethint.php 裡面的程式碼...

$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
........
// get the q parameter from URL
$q = $_REQUEST["q"];

// lookup all hints from array if $q is different from "" 
if ($q !== "") {
    $q = strtolower($q);
    $len=strlen($q);
    foreach($a as $name) {
        if (stristr($q, substr($name, 0, $len))) {
            if ($hint === "") {
                $hint = $name;
            } else {
                $hint .= ", $name";
            }
        }
    }
}

=======================================================
<html>
<head>
<script>
function showHint(str) {
    if (str.length == 0) { 
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET", "gethint.php?q=" + str, true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<p><b>Start typing a name in the input field below:</b></p>
<form> 
First name: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
=================================================================

gethink,php 的內容

=================================================================
<?php
// Array with names
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
$a[] = "Diana";
$a[] = "Eva";
$a[] = "Fiona";
$a[] = "Gunda";
$a[] = "Hege";
$a[] = "Inga";
$a[] = "Johanna";
$a[] = "Kitty";
$a[] = "Linda";
$a[] = "Nina";
$a[] = "Ophelia";
$a[] = "Petunia";
$a[] = "Amanda";
$a[] = "Raquel";
$a[] = "Cindy";
$a[] = "Doris";
$a[] = "Eve";
$a[] = "Evita";
$a[] = "Sunniva";
$a[] = "Tove";
$a[] = "Unni";
$a[] = "Violet";
$a[] = "Liza";
$a[] = "Elizabeth";
$a[] = "Ellen";
$a[] = "Wenche";
$a[] = "Vicky";

// get the q parameter from URL
$q = $_REQUEST["q"];

$hint = "";

// lookup all hints from array if $q is different from "" 
if ($q !== "") {
    $q = strtolower($q);
    $len=strlen($q);
    foreach($a as $name) {
        if (stristr($q, substr($name, 0, $len))) {
            if ($hint === "") {
                $hint = $name;
            } else {
                $hint .= ", $name";
            }
        }
    }
}

// Output "no suggestion" if no hint was found or output correct values 
echo $hint === "" ? "no suggestion" : $hint;
?>

=====================================================================


Reference : http://www.w3schools.com/php/php_ajax_php.asp