php的ajax簡單實例

當(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ù)并展示等等。
<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。
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ù)。
<?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;
?>
- ajax php 實現(xiàn)寫入數(shù)據(jù)庫
- php從數(shù)據(jù)庫中獲取數(shù)據(jù)用ajax傳送到前臺的方法
- php+ajax 實現(xiàn)輸入讀取數(shù)據(jù)庫顯示匹配信息
- Ajax PHP 邊學(xué)邊練 之三 數(shù)據(jù)庫
- PHP jQuery+Ajax結(jié)合寫批量刪除功能
- php+ajax實現(xiàn)圖片文件上傳功能實例
- php+ajax實現(xiàn)無刷新動態(tài)加載數(shù)據(jù)技術(shù)
- php采用ajax數(shù)據(jù)提交post與post常見方法總結(jié)
- ThinkPHP中使用ajax接收json數(shù)據(jù)的方法
- php+ajax+jquery實現(xiàn)點擊加載更多內(nèi)容
- PHP+jQuery+Ajax實現(xiàn)用戶登錄與退出
- php + ajax 實現(xiàn)的寫入數(shù)據(jù)庫操作簡單示例
相關(guān)文章

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

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

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