當前位置:首頁 » 編程語言 » php城市聯動

php城市聯動

發布時間: 2023-03-21 05:26:30

A. php 省市聯動如何提交表格JS選擇了沒有提交,謝謝

你發貨扒耐地址 標簽里值 和 下面 【 德陽市 什邡市 師古鎮 】 沒有連接上了 。。 post取得時候下面的值 不談此衫是你選的 select標簽里的值
用js聯動 這兩個 或含腔者 直接post select標簽里的值就行

B. php ajax關於省市聯動


$('option','#area').reomove();
$.each(data,function(index,ary){
$('#area').append("<optionvalue='"+ary['id']+"'>"+ary['countyName']+"</option>");
});

放在你的ajax方法里。

C. 關於php+mysql+ajax省市區三級聯動菜單,求幫助

基本思想就是:在JS動態創建select控制項的option,通過Ajax獲取在PHP從SQL資料庫獲取的省市區信息,代碼有點長,但很多都是類似的,例如JS中省、市、區獲取方法類似,PHP中通過參數不同執行不同的select語句。

index.html代碼:

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>省市區三級聯動</title>
<METAhttp-equiv=Content-Typecontent="text/html;charset=gb2312">
<scriptsrc="scripts/thumbnails.js"type="text/javascript"></script>
</head>

thumbnails.js代碼:

window.onload=getProvince;


functioncreateRequest(){//Ajax於PHP交互需要對象

try{

request=newXMLHttpRequest();//創建一個新的請求對象;

}catch(tryMS){

try{

request=newActiveXObject("Msxml2.XMLHTTP");

}catch(otherMS){

try{

request=newActiveXObject("Microsoft.XMLHTTP");

}catch(failed){

request=null;

}

}

}

returnrequest;

}


functionsech(id){//省市改變時觸發,select的onchange事件


varaa=document.getElementById(id);

if(id=="sheng"){

getCity(aa.value);//這里aa.value為省的id

}

if(id=="shi")

{

getCounty(aa.value);//這里aa.value為市的id

}


}


functiongetProvince(){//獲取所有省

request=createRequest();

if(request==null){

alert("Unabletocreaterequest");

return;

}

varurl="getDetails.php?ID=0";//ID=0時傳遞至PHP時讓其獲取所有省

request.open("GET",url,true);

request.onreadystatechange=displayProvince;//設置回調函數

request.send(null);//發送請求

}


functiongetCity(id){//獲取省對應的市

request=createRequest();

if(request==null){

alert("Unabletocreaterequest");

return;

}

varurl="getDetails.php?ID="+escape(id);

request.open("GET",url,true);

request.onreadystatechange=displayCity;

request.send(null);

}


functiongetCounty(id){//獲取市對應的區

request=createRequest();

if(request==null){

alert("Unabletocreaterequest");

return;

}

varurl="getDetails.php?ID="+escape(id);

request.open("GET",url,true);

request.onreadystatechange=displayCounty;

request.send(null);

}
functiondisplayProvince(){//將獲取的數據動態增加至select

if(request.readyState==4){

if(request.status==200){

vara=newArray;

varb=request.responseText;//將PHP返回的數據賦值給b

a=b.split(",");//通過","將這一數據保存在數組a中

document.getElementById("sheng").length=1;

varobj=document.getElementById("sheng');

for(i=0;i

obj.options.add(newOption(a[i],i+1));//動態生成OPTION加到select中,第一個參數為Text,第二個參數為Value值.


}

}

}
functiondisplayCity(){//將獲取的數據動態增加至select

if(request.readyState==4){

if(request.status==200){

vara=newArray;

varb=request.responseText;

a=b.split(",");

document.getElementById("shi").length=1;//重新選擇

document.getElementById("xian").length=1;//重新選擇

if(document.getElementById("sheng").value!="province"){

varobj=document.getElementById('shi');

for(i=0;i

obj.options.add(newOption(a[i],document.getElementById("sheng").value*100+i+1));//ocument.getElementById("sheng").value*100+i+1對應的是市的ID。

}


}

}

}


functiondisplayCounty(){//將獲取的數據增加至select

if(request.readyState==4){

if(request.status==200){

vara=newArray;

varb=request.responseText;

a=b.split(",");

document.getElementById("xian").length=1;

if(document.getElementById("sheng").value!="province"&&document.getElementById("shi").value!="city"){

varobj=document.getElementById('xian');

for(i=0;i

obj.options.add(newOption(a[i],i+1001));

}


}

}

}

getDetails.php代碼:

<?php

header("Content-Type:text/html;charset=gb2312");

$conn=newCOM("ADODB.Connection")ordie("CannotstartADO");

$connstr="Provider=SQLOLEDB;PersistSecurityInfo=False;UserID=root;Password=123456;InitialCatalog=area;DataSource=localhost";

if($_REQUEST['ID']==0){//獲得省列表
$conn->Open($connstr);//建立資料庫連接
$sqlstr="selectnamefromProvince";//設置查詢字元串
$rs=$conn->Execute($sqlstr);//執行查詢獲得結果
$num_cols=$rs->Fields->Count();//得到數據集列數
$Province=array();
$i=0;
while(!$rs->EOF){
$Province[$i]=$rs->Fields['name']->Value.",";
$rs->MoveNext();
$i++;
}
foreach($Provinceas$val)
echo$val;
$conn->Close();
$rs=null;
$conn=null;
}
if($_REQUEST['ID']>0&&$_REQUEST['ID']<35){//獲得省對應的市列表
$conn->Open($connstr);//建立資料庫連接
$sqlstr="selectnamefromCitywherecid=".$_REQUEST['ID'];//設置查詢字元串
$rs=$conn->Execute($sqlstr);//執行查詢獲得結果
$num_cols=$rs->Fields->Count();//得到數據集列數
$City=array();
$i=0;
while(!$rs->EOF){
$City[$i]=$rs->Fields['name']->Value.",";
$rs->MoveNext();
$i++;
}
foreach($Cityas$val)
echo$val;
$conn->Close();
$rs=null;
$conn=null;
}
if($_REQUEST['ID']>100){//獲得省市對應的縣列表
$conn->Open($connstr);//建立資料庫連接
$sqlstr="selectnamefromCountywherecid=".$_REQUEST['ID'];//設置查詢字元串
$rs=$conn->Execute($sqlstr);//執行查詢獲得結果
$num_cols=$rs->Fields->Count();//得到數據集列數
$County=array();
$i=0;
while(!$rs->EOF){
$County[$i]=$rs->Fields['name']->Value.",";
$rs->MoveNext();
$i++;
}
foreach($Countyas$val)
echo$val;
$conn->Close();
$rs=null;
$conn=null;
}
?>

資料庫設計,表格Province表,City表,County表。
要求:Province表需要id和name,id建議從1至34,例如北京id為1,廣東id為2,以此類推;
City表需要id,name和cid,id為cid*100+1,cid為該市的上級,例如深圳的上級為廣東省,cid為2的話,深圳的id就是201,以此類推。
County表需要id,name和cid,因為是三級的關系,id可以隨意,建議從10001開始自增。cid為所在上級,例如寶安區的cid為201,龍崗區的cid也為201;

截圖:

HTML效果:

D. PHP如何做一個選擇城市三級聯動後 跳轉到相應匹配內容,類似餓了么網頁主頁一樣:https://www.ele.me/home/

城市選擇後,應該就會按照你的城市進行檢索出,你對應地方的商家吧

E. phpcms的地區聯動 怎麼實現

這樣試試呢,在數據模型中創建自定義欄位(可參考iphpcms的教程)時欄位類型設置為「聯動菜單」,然後在「菜單id」選項處通過聯動菜單列表選擇你的地區聯動菜單,並可設定聯動菜單的返回值的方式。然後在內容添加編輯界面可出現此聯動菜單項,此方法常用。

二、在phpcms模板中直接調用聯動菜單,此方法不常用。具體方法是來到後台-擴展-聯動菜單列表,你會看到每一個聯動菜單都有一個調用代碼,將此代碼復制粘貼到你想放的模板位置即可。但根據經驗,這還不夠,因為雖然通過此代碼把聯動菜單掉出來了,但是因為缺乏此帶單顯示時的css、js文件,往往顯示不正常,所以需要保證你的模板里同時引入了statics/js/dialog.js、statics/js/linkage/js/pop.js、statics/css/dialog.css等文件。需要對phpcms有一定了解。如果不行的話你可以直接去後盾人線下面授培訓問問那些專家講師,他們很樂意幫你的,希望能幫到你,給個採納吧謝謝( ⌒㉨⌒)人(⌒㉨⌒ )v

F. php在一張表裡怎麼寫省市區聯動

我沒看懂你具體想干什麼。。。
我做過省市縣分級聯動下拉菜單,不知道是不是你想要的。。。

G. php 省市縣 三級聯動菜單 問題請教廣大網友

<?php
/***
根據省的id 選擇對應的城市
****/
$id = $_GET["id"];
$link = mysql_connect("localhost","root","root");
mysql_query("set names utf8");
mysql_select_db("hello",$link);
$sql = "select * from areas where pid={$id}";
$result = mysql_query($sql);
?>
<option value="-1">請選擇市</option>
<?php
while($rs = mysql_fetch_assoc($result)){
?>
<衫兆option value="<?php echo $rs["id"]?>"><?php echo $rs["name"]?></option>
<?php
}
?>

拼接的頁面大致這掘激樣

$("#pro").change(function(){
//$tcId=$(this).val();
$.ajax({
type:"GET",
url:"city.php?random="+Math.random()+"&tcId="+$(this).val(),
dataType:"html",
success:function(data){
$("#city").html(data);
}
})
}) 我的jquery是這樣寫的 你看看吧,應該都差不多的 檢查下拼接的判塌襪輸出

H. php二級聯動菜單

我給你提供一個三級的
主頁枝旅李面是
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>三級聯動下拉列表</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="/js/jquery.js"></script>鎮差
<script type="text/javascript">
<!--
var temp;
var ids = ['province','city','eare1']; //默認要操作的三個ID,注意先後順序,不可顛倒。
// 參數說明:pid是關聯 的id (第二個參數) 的父級,n表示是第幾級,(如第一級,第二級,第三級),selected 默認被選中的選擇的主鍵
function getList (pid,id,n,selected) {
var list = document.getElementById(id);
$.post ('ajax.php?act=getList',{'pid':pid,'action':id},function (data) {
var temp1 = eval('('+ data +')'); //把傳過來的字猛遲符串轉化成一個JSON對象。
var leng = temp1.length;

var n = (n > ids.length ) ? ids.length : n;
n = (n < 0 ) ? 0 : n;
for (var j = n ; j < ids.length ; j++)
{
var t = 'temp'+j
t = document.getElementById(ids[j]);
t.options.length = 1;
t.options[0]=new Option('請選擇','*');
}

if (leng > 0) {
list.length = leng + 1;
for (var i=0;i < temp1.length ;i++ )
{
list.options[i+1]=new Option(decodeURI(temp1[i].key),temp1[i].val);
if (temp1[i].region_id == selected ) {
list.options[0].selected = 'selected';
}
if (selected&&list.options[i+1].value==selected){
list.options[i+1].selected = 'selected';
}
}
}
if(pid == '*') {
switch(id){
case 'city':
t = document.getElementById('city');
t.options.length = 1;
t.options[0]=new Option('請選擇','*');
t = document.getElementById('eare1');
t.options.length = 1;
t.options[0]=new Option('請選擇','*');
break;
case 'eare1':
t = document.getElementById('eare1');
t.options.length = 1;
t.options[0]=new Option('請選擇','*');
break;
}
}
if(document.getElementById('city')&&document.getElementById('city').value=='*'){
t = document.getElementById('eare1');
t.options.length = 1;
t.options[0]=new Option('請選擇','*');
}
});
}
$(function () {
getList ('1','province',1);
//三個都寫是為了修改的時候,請三個框中默認的都有選中的值,一般增加的時候只寫第一個就可以了。
});
</script>
</head>

<body>
<div >
<select name="yc1" id="province" onchange="getList (this.value,'city',1)">
<option value="*" selected="selected">請選擇</option>
</select>

<select name="yc2" id="city" onchange="getList (this.value,'eare1',2)">
<option value="*" selected="selected">請選擇</option>
</select>

<select name="yc3" id="eare1">
<option value="*" selected="selected">請選擇</option>
</select>

</div>
</body>
</html>

AJAX 頁面是
<?php
$link = mysql_connect("localhost", "root", "123456");
mysql_select_db("mydatabase");
$act = isset ($_GET['act']) ? $_GET['act'] : '' ;
$action = isset ($_POST['action']) ? $_POST['action'] : '' ;
$pid = isset ($_POST['pid']) ? $_POST['pid'] : '' ;
$arr = array();
switch ($action) {
case 'province':
$sql = "select DISTINCT(province_name) val,province_id key from province order by id";
$res = mysql_query($sql);
while($col = mysql_fetch_array($res)){
$arr[] = $col;
}
break;
case 'city':
$sql = "select DISTINCT(city_name) val,city_id key from city where `province_id` = '".$pid."'
order by id";
$res = mysql_query($sql);
while($col = mysql_fetch_array($res)){
$arr[] = $col;
}
break;
case 'eare1':
$sql = "select DISTINCT(eare1_name) val,eare1_id key from eare1 where `city_id` = '".$pid."'
order by id";
$res = mysql_query($sql);
while($col = mysql_fetch_array($res)){
$arr[] = $col;
}
break;
}
mysql_close($link);
$list = array();
$i = 0;
foreach($arr as $k => $v){
foreach($v as $key => $value){
if(!preg_match("|^\d+|",$key)){
$list[$i][$key] = $value;
}
}
$i++;
}
print_r (json_encode ($list));

資料庫關聯是
表province 區域表
有ID province_id province_name三個欄位
id是自增的 province_id為唯一
表CITY 城市表
有ID city_id city_name province_id
其中province_id與province表的province_id對應
id是自增的 city_id為唯一
最後一個表類似與city表

jquery.js可以到http://jquery.com/下載

如果有問題可以在晚上7-10點 在H!給我留言

I. php網頁,搜索框怎麼加入城市的選項目前只有省份的,如何添加城市產生聯動

你這 是通過 後台取數據的,關改 上面的代碼是沒有用的,省市聯動一般有兩種做法。第一種,下載一個js的省 市聯動的插件,調用插件就可以,第二種,就是存在資料庫里,然後前端當省框的值改變 的時候就ajax去後台取出該省的所有市在市框中列出來,然後再市框的值改變的時候,再後台去取出該市所有的縣區在前端列出來。現在大部份是用第一種做法比較好,下個插件吧

J. 怎麼用php讀取資料庫方式動態生成省市縣三級聯動選擇框

需要使用到ajax。到網上查下,一堆。代碼是固定的。
資料庫設置
id,area,areaname
1 210000 遼寧省
2 210100 沈陽市
3 210104 大東區

<select name='area1' id='area1' onchange="get_area2(this.value);">
這里讀取省的數據
$sql="select area,areaname from area where right(area,4)='0000'";
</select>
<select name='area2' id='area2 'onchange="get_area(this.value);">
這里根據ajax讀取數據,開始的時候是空的
</select>
<select name='area' id='area'>
這里根據ajax讀取數據,開始的時候是空的
</select>

方法:
1,首先寫get_area2 的js 代碼,這里就用到ajax讀取,這里獲取的數據是區域代碼的前兩位代碼(比如:21)
這個在根據這個21的參數,讀取表中相關的市,
$sql="select area,areaname from area where left(area,2)='21' and right(area,2)='00'";
這個語句讀取出來21的市代碼

2,同一讀取出來區的代碼

熱點內容
編程畫櫻花 發布:2024-03-29 02:11:24 瀏覽:471
騰訊雲伺服器1mb老掉線 發布:2024-03-29 01:56:11 瀏覽:213
執行sql語句的存儲過程 發布:2024-03-29 01:52:37 瀏覽:695
婚紗攝影腳本 發布:2024-03-29 01:47:40 瀏覽:899
我的世界伺服器咋開外掛 發布:2024-03-29 01:07:45 瀏覽:455
sql寫報表 發布:2024-03-29 01:03:23 瀏覽:305
家用伺服器怎麼選 發布:2024-03-29 00:49:18 瀏覽:401
Ap6510dn如何配置 發布:2024-03-29 00:38:47 瀏覽:333
安卓和蘋果哪個更佔用內存 發布:2024-03-29 00:37:02 瀏覽:424
編譯錯誤算bug嗎 發布:2024-03-29 00:23:03 瀏覽:34