欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php的ajax簡單實例

 更新時間:2014年02月27日 08:58:41   作者:  
本篇文章主要是對php的ajax簡單實例進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助


當(dāng)輸入j后,會觸發(fā)ajax效果,從后臺獲取相應(yīng)的名字中帶有j的數(shù)據(jù),并展示在suggestions中。

代碼實現(xiàn)如下:

實現(xiàn)ajax需要三個文件,一個是html的表單文件,一個是js的核心文件,一個是php的后臺文件。

下面的是html文件,當(dāng)鍵盤按下時觸發(fā)showHint方法,在showHint方法中會有ajax的核心內(nèi)容,實例化,獲取地址,獲取數(shù)據(jù)并展示等等。

復(fù)制代碼 代碼如下:

<html>
<head>
<script src="clienthint.js"></script>
</head>

<body>

<form>
First Name:
<input type="text" id="txt1"
onkeyup="showHint(this.value)">
</form>

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

</body>
</html>

 

下面是js的內(nèi)容clienthint.js。

復(fù)制代碼 代碼如下:

var xmlHttp

function showHint(str)
{
if (str.length==0)
 {
 document.getElementById("txtHint").innerHTML=""
 return
 }
//獲取xmlHttpObject對象,如果為空,提示瀏覽器不支持ajax
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
 //獲取url
var url="gethint.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
 //回調(diào)函數(shù),執(zhí)行動作
xmlHttp.onreadystatechange=stateChanged
 //open
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
//將獲取的信息插入到txtHint中
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}


//獲取xml對象
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
 {
 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
 }
catch (e)
 {
 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
}
return xmlHttp;
}

下面是php的內(nèi)容。根據(jù)ajax對象傳入的參數(shù),獲取相應(yīng)的數(shù)據(jù)。

復(fù)制代碼 代碼如下:

<?php
// Fill up array with names
$a[]="Anna";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";
$a[]="Hege";
$a[]="Inga";
$a[]="Johanna";
$a[]="Jiqing";
$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=$_GET["q"];

//lookup all hints from array if length of 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];
     }
   }
 }
}

//Set output to "no suggestion" if no hint were found
//or to the correct values
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}

//output the response
echo $response;
?>

相關(guān)文章

  • Codeigniter中禁止A Database Error Occurred錯誤提示的方法

    Codeigniter中禁止A Database Error Occurred錯誤提示的方法

    在默認(rèn)的情況下,CodeIgniter會顯示所有的PHP錯誤。但是當(dāng)你開發(fā)程序結(jié)束時,你可能想要改變這個情況。這篇文章主要介紹了Codeigniter中禁止A Database Error Occurred錯誤提示的方法,需要的朋友可以參考下
    2014-06-06
  • php筆記之:php數(shù)組相關(guān)函數(shù)的使用

    php筆記之:php數(shù)組相關(guān)函數(shù)的使用

    本篇文章介紹了,php中數(shù)組相關(guān)函數(shù)的使用。需要的朋友參考下
    2013-04-04
  • php利用事務(wù)處理轉(zhuǎn)賬問題

    php利用事務(wù)處理轉(zhuǎn)賬問題

    這篇文章主要介紹了php利用事務(wù)處理轉(zhuǎn)賬問題的方法,實例分析了php處理事務(wù)的提交與回滾的技巧,需要的朋友可以參考下
    2015-04-04
  • 基于Laravel(5.4版本)的基本增刪改查操作方法

    基于Laravel(5.4版本)的基本增刪改查操作方法

    今天小編就為大家分享一篇基于Laravel(5.4版本)的基本增刪改查操作方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • php使用smtp發(fā)送支持附件的郵件示例

    php使用smtp發(fā)送支持附件的郵件示例

    這篇文章主要介紹了php使用smtp發(fā)送支持附件的郵件示例,需要有smtp服務(wù)器,代碼經(jīng)過多次實戰(zhàn)使用,需要的朋友可以參考下
    2014-04-04
  • PHP中的數(shù)組分頁實現(xiàn)(非數(shù)據(jù)庫)實例講解

    PHP中的數(shù)組分頁實現(xiàn)(非數(shù)據(jù)庫)實例講解

    這篇文章主要介紹了PHP中的數(shù)組分頁實現(xiàn)(非數(shù)據(jù)庫)實例講解,實例講解的很清楚,有對這方面有需要的同學(xué)可以借鑒下
    2021-01-01
  • php/js獲取客戶端mac地址的實現(xiàn)代碼

    php/js獲取客戶端mac地址的實現(xiàn)代碼

    這篇文章主要介紹了如何在php與js中分別獲取客戶度mac地址的方法,需要的朋友可以參考下
    2013-07-07
  • 最新評論