浣跨敤 PHP+AJAX 鎶鏈浠庢暟鎹搴撹诲彇淇℃伅

2013 年 10 月 30 日4680

AJAX瀹炰緥瑙i噴
涓婇潰鐨勫疄渚嬪寘鍚浜嗕竴涓绠鍗曠殑HTML琛ㄥ崟浠ュ強鎵цJavaScript鐨勯摼鎺ワ細

<html>
<head>
<title>shuro' blog</title>
<scriptsrc="selectcustomer.js"></script>
</head>

<body>

<form>
璇烽夋嫨涓浣嶅㈡埛锛
<selectname="customers"onchange="showCustomer(this.value)">
<optionvalue="ALFKI">AlfredsFutterkiste
<optionvalue="NORTS">North/South
<optionvalue="WOLZA">WolskiZajazd
</select>
</form>

<p>
<divid="txtHint"><b>瀹㈡埛淇℃伅灏嗗湪姝ゅ勫垪鍑恒</b></div>
</p>

</body>
</html>
姝e傛偍鐪嬪埌鐨勶紝杩欐槸涓涓绠鍗曠殑甯︽湁涓涓鍚嶄负"customers"涓嬫媺鍒楄〃鐨HTML琛ㄥ崟銆

琛ㄥ崟浠ヤ笅鐨勬佃惤鍖呭惈浜嗕竴涓鍚嶄负"txtHint"鐨div锛岃繖涓div鍏呭綋浜嗙敱web鏈嶅姟鍣ㄦ墍鍙栧洖鐨勪俊鎭鐨勪綅缃鍗犱綅绗︺

褰撶敤鎴烽夋嫨鏁版嵁鏃讹紝鍚嶄负"showCustomer()"鐨勫嚱鏁颁細琚鎵ц屻傚嚱鏁扮殑鎵ц屼細琚"onchange"浜嬩欢瑙﹀彂銆傚彟澶栭渶瑕佽存槑鐨勬槸锛氭瘡褰撶敤鎴锋敼鍙樹笅鎷夊垪琛ㄤ腑鐨勫硷紝鍑芥暟showCustomer灏变細琚璋冪敤銆

涓嬮潰鍒楀嚭浜JavaScript浠g爜銆
AJAXJavaScript
杩欐槸瀛樺偍鍦ㄦ枃浠"selectcustomer.js"涓鐨JavaScript浠g爜锛

varxmlHttp

functionshowCustomer(str)
{
xmlHttp=GetXmlHttpObject();
if(xmlHttp==null)
{
alert("鎮ㄧ殑娴忚堝櫒涓嶆敮鎸丄JAX锛");
return;
}
varurl="getcustomer.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

functionstateChanged()
{
if(xmlHttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}

functionGetXmlHttpObject()
{
varxmlHttp=null;
try
{
//Firefox,Opera8.0+,Safari
xmlHttp=newXMLHttpRequest();
}
catch(e)
{
//InternetExplorer
try
{
xmlHttp=newActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
xmlHttp=newActiveXObject("Microsoft.XMLHTTP");
}
}
returnxmlHttp;
}
AJAX鏈嶅姟鍣ㄩ〉闈
杩欎釜琚JavaScript璋冪敤鐨勬湇鍔″櫒椤甸潰锛屾槸涓涓鍚嶄负"getcustomer.php"鐨勭畝鍗曠殑php鏂囦欢銆

姝や唬鐮佸彲杩愯岄拡瀵规煇涓鏁版嵁搴撶殑SQL锛屽苟浠HTML琛ㄦ牸杩斿洖缁撴灉
<?php
$link=mysql_connect("localhost","root","123456")ordie("鐢ㄦ埛鍚嶅瘑鐮侀敊璇");
mysql_select_db("join_link",$link)ordie("鏈杩炴帴涓婃暟鎹搴");
$sql="select*fromregisterwherereg_id=".$_GET['q'];
$result=mysql_query($sql);
while($row=mysql_fetch_array($result))
{
echo"regid=".$row['reg_id']."<br/>";
echo"regpwd=".$row['reg_pwd']."<br/>";
}
echo"sid=".$_GET['sid'];
//echo"<inputtype='text'/>";
mysql_close($link);
?>

鏈鏂囩珷鍑鸿嚜Shuro's Blog锛http://http://www.zjjv.com//锛夛紝杞杞借锋敞鏄庡嚭澶勶紝璋㈣阿锛

0 0