php的ajax簡(jiǎn)單實(shí)例

當(dāng)輸入j后,會(huì)觸發(fā)ajax效果,從后臺(tái)獲取相應(yīng)的名字中帶有j的數(shù)據(jù),并展示在suggestions中。
代碼實(shí)現(xiàn)如下:
實(shí)現(xiàn)ajax需要三個(gè)文件,一個(gè)是html的表單文件,一個(gè)是js的核心文件,一個(gè)是php的后臺(tái)文件。
下面的是html文件,當(dāng)鍵盤按下時(shí)觸發(fā)showHint方法,在showHint方法中會(huì)有ajax的核心內(nèi)容,實(shí)例化,獲取地址,獲取數(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對(duì)象,如果為空,提示瀏覽器不支持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í)行動(dòng)作
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對(duì)象
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對(duì)象傳入的參數(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 實(shí)現(xiàn)寫入數(shù)據(jù)庫(kù)
- php從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)用ajax傳送到前臺(tái)的方法
- php+ajax 實(shí)現(xiàn)輸入讀取數(shù)據(jù)庫(kù)顯示匹配信息
- Ajax PHP 邊學(xué)邊練 之三 數(shù)據(jù)庫(kù)
- PHP jQuery+Ajax結(jié)合寫批量刪除功能
- php+ajax實(shí)現(xiàn)圖片文件上傳功能實(shí)例
- php+ajax實(shí)現(xiàn)無刷新動(dòng)態(tài)加載數(shù)據(jù)技術(shù)
- php采用ajax數(shù)據(jù)提交post與post常見方法總結(jié)
- ThinkPHP中使用ajax接收json數(shù)據(jù)的方法
- php+ajax+jquery實(shí)現(xiàn)點(diǎn)擊加載更多內(nèi)容
- PHP+jQuery+Ajax實(shí)現(xiàn)用戶登錄與退出
- php + ajax 實(shí)現(xiàn)的寫入數(shù)據(jù)庫(kù)操作簡(jiǎn)單示例
相關(guān)文章

Codeigniter中禁止A Database Error Occurred錯(cuò)誤提示的方法

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

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

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