澳门新葡萄京官网注册落伍首发 php+mysql 采用ajax技术的 省 市 地 3级联动无刷新菜单 源码_php实例_脚本之家

htmlheadmeta;varrequestType=;functioncreateXMLHttpRequest(){if(window.ActiveXObject){xmlHttp=newActiveXObject(Microsoft.XMLHTTP);}elseif(window.XMLHttpRequest){xmlHttp=newXMLHttpRequest();}}functionqueryCity(citycode){createXMLHttpRequest();type=city;varurl=data.php?provincecode=+citycode;xmlHttp.open(GET,url,true);xmlHttp.onreadystatechange=handleStateChange;xmlHttp.send(null);}functionqueryArea(citycode){createXMLHttpRequest();type=area;varurl=data.php?citycode=+citycode;xmlHttp.open(GET,url,true);xmlHttp.onreadystatechange=handleStateChange;xmlHttp.send(null);}functionhandleStateChange(){if(xmlHttp.readystate==4){if(xmlHttp.status==200){if(type==city){showcity();}elseif(type=area){showarea();}}}}functionshowcity(){document.getElementById(city).innerHTML=xmlHttp.responseText;document.getElementById(area).innerHTML=;}functionshowarea(){document.getElementById(area).innerHTML=xmlHttp.responseText;}/script/headbody?$conn=mysql_connect(localhost,root,2328725);mysql_select_db(novel);mysql_query(setnames’utf8′);$sql=select*fromprovince;$result=mysql_query($sql);echofromid=’form1’/n;echoselectid=’province’onchange=’queryCity(this.options[this.selectedIndex].value)’/n;echooptionvalue=’-1’selected请选择省份/option/n;while($row=mysql_fetch_row($result)){echooptionvalue=’$row[1]’$row[2]/option/n;}echo/select/n;echospanid=’city’/span/n;echospanid=’area’/span/n;echo/form/n;?/body/html

绝对原创 测试地址:
测试页代码:复制代码 代码如下:

php+ajax动态生成下拉菜单

regionss函数代码:QUOTE: 复制代码
代码如下://省市地3级联动下拉菜单函数regionss省.市.地 functionregionss {
global$db;$i=1; $text=”省/市/自治区n”;
$result=$db->query(“SELECTprovinceFROM”.TABLE_PROVINCE.”WHEREcountry=’$country’ORDERBYprovinceid”);
while($r=$db->fetch_array { $text.=””.$r[‘province’].””; $i++; }
$text.=””; $text.=”&nbsp市/县/区n”; $text.=”县级市/县n”;
return$text; }regions.js的代码: 复制代码
代码如下://varab=newArray(); varxmlHttp; varxmlHttpa;
functioncreateXMLHttpRequest(){ if{ xmlHttp=newActiveXObject; }
elseif(window.XMLHttpRequest){ xmlHttp=newXMLHttpRequest(); } }
functioncreateXMLHttpRequesta(){ if{ xmlHttpa=newActiveXObject; }
elseif(window.XMLHttpRequest){ xmlHttpa=newXMLHttpRequest(); } }
functionstartRequest(){ createXMLHttpRequest(); createXMLHttpRequesta();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttpa.onreadystatechange=handleStateChangearea;
document.getElementById.options.length=0;
document.getElementById.options.length=0;
varurl=document.form1.select1.value;
varqurl=”/regions.php?province=”+url+”&time=”+newDate; xmlHttp.open;
xmlHttp.send; //setTimeout”,2000); } functionhandleStateChange(){
if(xmlHttp.readyState==4){ if{
varobj=document.getElementById;//将服务器返回的字符串写到页面中ID为select2的区域
obja=document.getElementById;//将服务器返回的字符串写到页面中ID为select3的区域
eval; } } } functionhandleStateChangearea(){
eval(xmlHttpa.responseText); } //varab=newArray();
functionstartRequesta(){ createXMLHttpRequesta();
xmlHttpa.onreadystatechange=handleStateChangea;
document.getElementById.options.length=0;
varurl=document.form1.select2.value;
varqurl=”/regions.php?city=”+url+”&time=”+newDate; xmlHttpa.open;
xmlHttpa.send; //setTimeout”,2000); } functionhandleStateChangea(){
if(xmlHttpa.readyState==4){ if{ varobja=document.getElementById;
eval(xmlHttpa.responseText);
//将服务器返回的字符串写到页面中ID为select3的区域 } }
}数据岛regions.php页面代码 复制代码
代码如下:

query(“SELECTDISTINCTcityFROM”.TABLE_CITY.”WHEREprovince=’$province’ORDERBYcityid”);
while($r=$db->fetch_array {
$r[city]=iconv(‘gb2312′,’UTF-8’,$r[city]);
echo”obj.options[obj.options.length]=newOption(‘”.$r[city].”‘,'”.$r[city].”‘);n”;
$i++; }
$resultarea=$db->query(“SELECTDISTINCTcityFROM”.TABLE_CITY.”WHEREprovince=’$province’ORDERBYcityid”);
$r=$db->fetch_array; $city=$r[city];
$resulta=$db->query(“SELECTDISTINCTareaFROM”.TABLE_CITY.”WHEREcity=’$city’ORDERBYcityid”);
while($ra=$db->fetch_array {
$ra[area]=iconv(‘gb2312′,’UTF-8’,$ra[area]);
echo”obja.options[obja.options.length]=newOption(‘”.$ra[area].”‘,'”.$ra[area].”‘);n”;
$i++; } } if{ global$db;$i=1;
$result=$db->query(“SELECTDISTINCTareaFROM”.TABLE_CITY.”WHEREcity=’$city’ORDERBYcityid”);
while($r=$db->fetch_array {
$r[area]=iconv(‘gb2312′,’UTF-8’,$r[area]);
echo”obja.options[obja.options.length]=newOption(‘”.$r[area].”‘,'”.$r[area].”‘);n”;
$i++; } } ?>地址信息数据库:
采用的是phpcms3.0里自带的数据库,我没有进行任何改动
本程序可以在phpcms3.0里直接使用,若在其他地方使用请自己修改

发表评论

电子邮件地址不会被公开。 必填项已用*标注