PHP - AJAX 與 PHP

來源:互聯網
上載者:User

標籤:題解   activex   win   res   bsp   應用   soft   設定   zab   

AJAX 被用於建立互動性更強的應用程式。

AJAX PHP 執行個體

下面的執行個體將示範當使用者在輸入框中鍵入字元時,網頁如何與 Web 服務器進行通訊:

 執行個體解釋 - HTML 頁面

當使用者在上面的輸入框中鍵入字元時,會執行 "showHint()" 函數。該函數由 "onkeyup" 事件觸發:

  1. <html>
    <head>
    <script>
    function showHint(str)
    {
    if (str.length==0)
    {
    document.getElementById("txtHint").innerHTML="";
    return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    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>在輸入框中輸入一個姓名:</b></p>
    <form>
    姓名: <input type="text" onkeyup="showHint(this.value)">
    </form>
    <p>傳回值: <span id="txtHint"></span></p>

    </body>
    </html>
複製

原始碼解釋:

如果輸入框是空的(str.length==0),該函數會清空 txtHint 預留位置的內容,並退出該函數。

如果輸入框不是空的,那麼 showHint() 會執行以下步驟:

  • 建立 XMLHttpRequest 對象
  • 建立在伺服器響應就緒時執行的函數
  • 向伺服器上的檔案發送請求
  • 請注意添加到 URL 末端的參數(q)(包含輸入框的內容)
PHP 檔案

上面這段通過 JavaScript 調用的伺服器頁面是名為 "gethint.php" 的 PHP 檔案。

"gethint.php" 中的原始碼會檢查名稱數組,然後向瀏覽器返回對應的姓名:

  1. <?php
    // 將姓名填充到數組中
    $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";

    //從請求URL地址中擷取 q 參數
    $q=$_GET["q"];

    //尋找是否由匹配值, 如果 q>0
    if (strlen($q) > 0)
    {
    $hint="";
    for($i=0; $i<count($a); $i++)
    {
    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
    {
    if ($hint=="")
    {
    $hint=$a[$i];
    }
    else
    {
    $hint=$hint." , ".$a[$i];
    }
    }
    }
    }

    // 如果沒有匹配值設定輸出為 "no suggestion"
    // or to the correct values
    if ($hint == "")
    {
    $response="no suggestion";
    }
    else
    {
    $response=$hint;
    }

    //輸出傳回值
    echo $response;
    ?>
複製

解釋:如果 JavaScript 發送了任何文本(即 strlen($q) > 0),則會發生:

  1. 尋找匹配 JavaScript 發送的字元的姓名
  2. 如果未找到匹配,則將響應字串設定為 "no suggestion"
  3. 如果找到一個或多個匹配姓名,則用所有姓名設定響應字串
  4. 把響應發送到 "txtHint" 預留位置
PHP Ajax 跨域問題解決方案

如果你的非同步請求需要跨域可以查看:PHP Ajax 跨域問題解決方案。

PHP - AJAX 與 PHP

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.