用Ajax写的省市联动,添加地址的时候默认的省市都显示,- -请选择- - 但是修改的时候如何让省市下拉菜单里默认的选中,地址里的省市?[下拉菜单里的省,是直接从数据库拿出来保存在一个数组变量里,把变量付给下拉框]
js代码:
<script> <br /> //创建Ajax引擎对象 <br /> function getXmlHttpObject(){ <br /> var xmlHttpRequest; <br /> if(window.ActiveXObject){ <br /> xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP"); <br /> }else{ <br /> xmlHttpRequest=new XMLHttpRequest(); <br /> } <br /> return xmlHttpRequest; <br /> } <br /> <br /> var myXmlHttpRequest=""; <br /> <br /> <br /> <br /> function getCities(){ <br /> <br /> myXmlHttpRequest=getXmlHttpObject(); <br /> <br /> if(myXmlHttpRequest){ <br /> <br /> var url="../plus/showcity.php";//post <br /> <br /> var data="province="+$("sheng").value; <br /> <br /> myXmlHttpRequest.open("post",url,true); <br /> <br /> myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); <br /> <br /> myXmlHttpRequest.onreadystatechange=chuli; <br /> <br /> myXmlHttpRequest.send(data); <br /> <br /> <br /> } <br /> } <br /> <br /> function chuli(){ <br /> <br /> if(myXmlHttpRequest.readyState==4){ <br /> <br /> if(myXmlHttpRequest.status==200){ <br /> //取出服务器回送的数据 <br /> //window.alert(myXmlHttpRequest.responseXML); <br /> var cities=myXmlHttpRequest.responseXML.getElementsByTagName("city"); <br /> //alert(cities.length); <br /> //遍历并取出城市 <br /> $("city").length=0; <br /> var myOption=document.createElement("option"); <br /> myOption.innerHTML="--请选择--"; <br /> $("city").appendChild(myOption); <br /> <br /> for(var i=0;i<cities.length;i++){ <br /> var city_name=cities[i].childNodes[0].nodeValue; <br /> //alert(city_name); <br /> var myOption=document.createElement("option"); <br /> myOption.value=city_name; <br /> myOption.innerHTML=city_name; <br /> $("city").appendChild(myOption); <br /> } <br /> } <br /> } <br /> } <br /> <br /> function $(id){ <br /> <br /> return document.getElementById(id); <br /> } <br /> </script>
html代码:
php文件:
require_once (dirname(__FILE__) . "/../include/common.inc.php");
require_once(DEDEINC.'/dedesql.class.php');
//服务器端
//告诉浏览器返回的数据格式是xml格式
header("Content-Type: text/xml;charset=utf-8");
//告诉浏览器不要缓存数据
header("Cache-Control: no-cache");
//接收用户选择省的名字
$province=$_POST['province'];
$city = array();
$dsql->SetQuery("SELECT * FROM area WHERE reid=$province");
$dsql->Execute();
$i = 0 ;
while($row=$dsql->GetArray())
{
$city[] = $row;
$i++;
}
unset($row);
$info="
$len=count($city);
for($l=0;$l //file_put_contents("e:/aa.txt",$city[$l][name],FILE_APPEND);
$info.="
}
$info.="
file_put_contents("e:/aa.txt",$info,FILE_APPEND);
echo $info;
回复讨论(解决方案)
选中 省 ajax 取得 市 列表
并不需要再修改 省 列表了
怎么选中省呢??
那我就奇怪了,你不是 省市联动 吗?
选中省 当然是用户的事情啦
Ajax实现数据库填充省市下拉框联动菜单示例
//Ajax和DropDownList设置
//调用数据库显示下拉框数据
SqlConnection sqlcon;
string strCon = ConfigurationManager.AppSettings["conn"];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "select tbSheng from tb_Sheng";
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds);
dltbSheng.DataSource= myds;
dltbSheng.DataValueField = "tbSheng";
dltbSheng.DataBind();
string strtb_Shi = "select * from dl_Shi where tbSheng='" + dltbSheng.SelectedItem.Text + "'";
SqlDataAdapter mydaShi = new SqlDataAdapter(strtb_Shi, sqlcon);
DataSet mydsShi = new DataSet();
mydaShi.Fill(mydsShi);
ddlShi.DataSource = mydsShi;
ddlShi.DataValueField = "tb_Shi";
ddlShi.DataBind();
sqlcon.Close();
}
}
protected void dlSheng_SelectedIndexChanged(object sender, EventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "select * from dl_Shi where tbSheng='" + dltbSheng.SelectedItem.Text+ "'";
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds);
ddlShi.DataSource = myds;
ddlShi.DataValueField = "tb_Shi";
ddlShi.DataBind();
sqlcon.Close();
}

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
