ajax网站源码分享?ajax web

今天给各位分享ajax网站源码分享的知识,其中也会对ajax web进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

AJAX用于创造动态性更强的应用程序。

AJAXASP/PHP实例

下面的例子将为您演示当用户在输入框中键入字符时,网页如何与web服务器进行通信:请在下面的输入框中键入字母(A-Z):

实例

Starttypinganameintheinputfieldbelow:

Firstname:

Suggestions:

实例解析-showHint()函数

当用户在上面的输入框中键入字符时,会执行函数”showHint()”。该函数由”onkeyup”事件触发:

functionshowHint(str)

{

varxmlhttp;

if(str.length==0)

{

document.getElementById(“txtHint”).innerHTML=””;

return;

}

if(window.XMLHttpRequest)

{//codeforIE7+,Firefox,Chrome,Opera,Safari

xmlhttp=newXMLHttpRequest();

}

else

{//codeforIE6,IE5

xmlhttp=newActiveXObject(“Microsoft.XMLHTTP”);

}

xmlhttp.onreadystatechange=function()

{

if(xmlhttp.readyState==4&&xmlhttp.status==200)

{

document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;

}

}

xmlhttp.open(“GET”,”gethint.html?q=”+str,true);

xmlhttp.send();

}

源代码解析:

如果输入框为空(str.length==0),则该函数清空txtHint占位符的内容,并退出函数。

如果输入框不为空,showHint()函数执行以下任务:

创建XMLHttpRequest对象

当服务器响应就绪时执行函数

把请求发送到服务器上的文件

请注意我们向URL添加了一个参数q(带有输入框的内容)

AJAX服务器页面-ASP和PHP

由上面的JavaScript调用的服务器页面是ASP文件,名为”gethint.asp”。

下面,我们创建了两个版本的服务器文件,一个用ASP编写,另一个用PHP编写。

ASP文件

“gethint.asp”中的源代码会检查一个名字数组,然后向浏览器返回相应的名字:

<%

response.expires=-1

dima(30)

‘Filluparraywithnames

a(1)=”Anna”

a(2)=”Brittany”

a(3)=”Cinderella”

a(4)=”Diana”

a(5)=”Eva”

a(6)=”Fiona”

a(7)=”Gunda”

a(8)=”Hege”

a(9)=”Inga”

a(10)=”Johanna”

a(11)=”Kitty”

a(12)=”Linda”

a(13)=”Nina”

a(14)=”Ophelia”

a(15)=”Petunia”

a(16)=”Amanda”

a(17)=”Raquel”

a(18)=”Cindy”

a(19)=”Doris”

a(20)=”Eve”

a(21)=”Evita”

a(22)=”Sunniva”

a(23)=”Tove”

a(24)=”Unni”

a(25)=”Violet”

a(26)=”Liza”

a(27)=”Elizabeth”

a(28)=”Ellen”

a(29)=”Wenche”

a(30)=”Vicky”

‘gettheqparameterfromURL

q=ucase(request.querystring(“q”))

‘lookupallhintsfromarrayiflengthofq>0

iflen(q)>0then

hint=””

fori=1to30

ifq=ucase(mid(a(i),1,len(q)))then

ifhint=””then

hint=a(i)

else

hint=hint&”,”&a(i)

endif

endif

next

endif

‘Output”nosuggestion”ifnohintwerefound

‘oroutputthecorrectvalues

ifhint=””then

response.write(“nosuggestion”)

else

response.write(hint)

endif

%>

PHP文件

下面的代码用PHP编写,与上面的ASP代码作用是一样的。

<?php

//Filluparraywithnames

$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”;

//gettheqparameterfromURL

$q=$_GET[“q”];

//lookupallhintsfromarrayiflengthofq>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];

}

}

}

}

//Setoutputto”nosuggestion”ifnohintwerefound

//ortothecorrectvalues

if($hint==””)

{

$response=”nosuggestion”;

}

else

{

$response=$hint;

}

//outputtheresponse

echo$response;

?>

如您还有不明白的可以在下面与我留言或是与我探讨QQ群308855039,我们一起飞!

ajax网站源码分享的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于ajax web、ajax网站源码分享的信息别忘了在本站进行查找哦。

Published by

风君子

独自遨游何稽首 揭天掀地慰生平