Home >Backend Development >PHP Problem >How to implement cascade in php

How to implement cascade in php

藏色散人
藏色散人Original
2020-10-16 09:53:332175browse

php method to implement cascading: first create the fun.js file and send a GET request to the server; then create index.php and use jQuery to make a cascading drop-down selection box to select the region; finally create returnpc. php and pc.php will do.

How to implement cascade in php

Recommended: "PHP Video Tutorial"

jQuery ajax PHP implemented cascading drop-down list box function:

一码

fun.js:

// JavaScript Document
$(document).ready(
 function(){
 $.get("returnpc.php?flag=p", null, function(data){ //向服务器发送GET请求,获取省份的值,并将结果追加到省份下拉列表中
   $("#p").append(data);
 });
 $("#c").css("display","none"); //初始状态使城市下拉列表不可见
 $("#p").change(function(){ //为省份下拉列表增加改变事件
   if($("#p").val()==""){ //在没选择省份的情况下,使城市下拉列表不可见
    $("#c").css("display","none");
  }else{
     $.get("returnpc.php?flag=c&p="+$("#p").val(), null, function(data){ //如果选择了某省份,则向服务器发送GET请求,使用回调函数为城市下拉列表赋值,并使城市下拉列表可见
      $("#c").css("display","");
   $("#c").empty();
      $("#c").append(data); //将数据追加到城市下拉列表
    });
    }
 });
  }
);

index.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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>使用jQuery制作级联下拉选择框选择地区</title>
</head>
<script language="javascript" src="js/jquery-1.3.2.js"></script>
<script language="javascript" src="js/fun.js"></script>
<body>
<select id="p" name="p" >
  <option value="">-请选择地区-</option>
</select>
<select id="c" name="c">
</select>
</body>
</html>

pc.php :

<?php
/* 说明: 全国(省,直辖市,自治区,特别行政区)数组*/
$p = array(&#39;北京&#39;,&#39;上海&#39;,&#39;天津&#39;,&#39;重庆&#39;,&#39;黑龙江&#39;,&#39;吉林&#39;,&#39;辽宁&#39;,&#39;内蒙古&#39;,&#39;河北&#39;,&#39;河南&#39;,&#39;山东&#39;,&#39;山西&#39;,&#39;江苏&#39;,&#39;安徽&#39;,&#39;陕西&#39;,&#39;宁夏&#39;,&#39;甘肃&#39;,&#39;青海&#39;,&#39;湖北&#39;,&#39;湖南&#39;,&#39;浙江&#39;,&#39;江西&#39;,&#39;福建&#39;,&#39;贵州&#39;,&#39;四川&#39;,&#39;广东&#39;,&#39;广西&#39;,&#39;云南&#39;,&#39;海南&#39;,&#39;新疆&#39;);
/* 说明: 省,市,直辖市,自治区所属的城市数组*/
$c = array();
$c[0] = array(&#39;北京&#39;);
$c[1] = array(&#39;上海&#39;);
$c[2] = array(&#39;天津&#39;);
$c[3] = array(&#39;重庆&#39;);
$c[4] = array(&#39;哈尔滨&#39;,&#39;齐齐哈尔&#39;,&#39;牡丹江&#39;,&#39;大庆&#39;,&#39;伊春&#39;,&#39;双鸭山&#39;,&#39;鹤岗&#39;,&#39;鸡西&#39;,&#39;佳木斯&#39;,&#39;七台河&#39;,&#39;黑河&#39;,&#39;绥化&#39;,&#39;大兴安岭&#39;);
$c[5] = array(&#39;长春&#39;,&#39;延边&#39;,&#39;吉林&#39;,&#39;白山&#39;,&#39;白城&#39;,&#39;四平&#39;,&#39;松原&#39;,&#39;辽源&#39;,&#39;大安&#39;,&#39;通化&#39;);
$c[6] = array(&#39;沈阳&#39;,&#39;大连&#39;,&#39;葫芦岛&#39;,&#39;旅顺&#39;,&#39;本溪&#39;,&#39;抚顺&#39;,&#39;铁岭&#39;,&#39;辽阳&#39;,&#39;营口&#39;,&#39;阜新&#39;,&#39;朝阳&#39;,&#39;锦州&#39;,&#39;丹东&#39;,&#39;鞍山&#39;);
$c[7] = array(&#39;呼和浩特&#39;,&#39;呼伦贝尔&#39;,&#39;锡林浩特&#39;,&#39;包头&#39;,&#39;赤峰&#39;,&#39;海拉尔&#39;,&#39;乌海&#39;,&#39;鄂尔多斯&#39;,&#39;通辽&#39;);
$c[8] = array(&#39;石家庄&#39;,&#39;唐山&#39;,&#39;张家口&#39;,&#39;廊坊&#39;,&#39;邢台&#39;,&#39;邯郸&#39;,&#39;沧州&#39;,&#39;衡水&#39;,&#39;承德&#39;,&#39;保定&#39;,&#39;秦皇岛&#39;);
$c[9] = array(&#39;郑州&#39;,&#39;开封&#39;,&#39;洛阳&#39;,&#39;平顶山&#39;,&#39;焦作&#39;,&#39;鹤壁&#39;,&#39;新乡&#39;,&#39;安阳&#39;,&#39;濮阳&#39;,&#39;许昌&#39;,&#39;漯河&#39;,&#39;三门峡&#39;,&#39;南阳&#39;,&#39;商丘&#39;,&#39;信阳&#39;,&#39;周口&#39;,&#39;驻马店&#39;);
$c[10] = array(&#39;济南&#39;,&#39;青岛&#39;,&#39;淄博&#39;,&#39;威海&#39;,&#39;曲阜&#39;,&#39;临沂&#39;,&#39;烟台&#39;,&#39;枣庄&#39;,&#39;聊城&#39;,&#39;济宁&#39;,&#39;菏泽&#39;,&#39;泰安&#39;,&#39;日照&#39;,&#39;东营&#39;,&#39;德州&#39;,&#39;滨州&#39;,&#39;莱芜&#39;,&#39;潍坊&#39;);
$c[11] = array(&#39;太原&#39;,&#39;阳泉&#39;,&#39;晋城&#39;,&#39;晋中&#39;,&#39;临汾&#39;,&#39;运城&#39;,&#39;长治&#39;,&#39;朔州&#39;,&#39;忻州&#39;,&#39;大同&#39;,&#39;吕梁&#39;);
$c[12] = array(&#39;南京&#39;,&#39;苏州&#39;,&#39;昆山&#39;,&#39;南通&#39;,&#39;太仓&#39;,&#39;吴县&#39;,&#39;徐州&#39;,&#39;宜兴&#39;,&#39;镇江&#39;,&#39;淮安&#39;,&#39;常熟&#39;,&#39;盐城&#39;,&#39;泰州&#39;,&#39;无锡&#39;,&#39;连云港&#39;,&#39;扬州&#39;,&#39;常州&#39;,&#39;宿迁&#39;);
$c[13] = array(&#39;合肥&#39;,&#39;巢湖&#39;,&#39;蚌埠&#39;,&#39;安庆&#39;,&#39;六安&#39;,&#39;滁州&#39;,&#39;马鞍山&#39;,&#39;阜阳&#39;,&#39;宣城&#39;,&#39;铜陵&#39;,&#39;淮北&#39;,&#39;芜湖&#39;,&#39;毫州&#39;,&#39;宿州&#39;,&#39;淮南&#39;,&#39;池州&#39;);
$c[14] = array(&#39;西安&#39;,&#39;韩城&#39;,&#39;安康&#39;,&#39;汉中&#39;,&#39;宝鸡&#39;,&#39;咸阳&#39;,&#39;榆林&#39;,&#39;渭南&#39;,&#39;商洛&#39;,&#39;铜川&#39;,&#39;延安&#39;);
$c[15] = array(&#39;银川&#39;,&#39;固原&#39;,&#39;中卫&#39;,&#39;石嘴山&#39;,&#39;吴忠&#39;);
$c[16] = array(&#39;兰州&#39;,&#39;白银&#39;,&#39;庆阳&#39;,&#39;酒泉&#39;,&#39;天水&#39;,&#39;武威&#39;,&#39;张掖&#39;,&#39;甘南&#39;,&#39;临夏&#39;,&#39;平凉&#39;,&#39;定西&#39;,&#39;金昌&#39;);
$c[17] = array(&#39;西宁&#39;,&#39;海北&#39;,&#39;海西&#39;,&#39;黄南&#39;,&#39;果洛&#39;,&#39;玉树&#39;,&#39;海东&#39;,&#39;海南&#39;);
$c[18] = array(&#39;武汉&#39;,&#39;宜昌&#39;,&#39;黄冈&#39;,&#39;恩施&#39;,&#39;荆州&#39;,&#39;神农架&#39;,&#39;十堰&#39;,&#39;咸宁&#39;,&#39;襄樊&#39;,&#39;孝感&#39;,&#39;随州&#39;,&#39;黄石&#39;,&#39;荆门&#39;,&#39;鄂州&#39;);
$c[19] = array(&#39;长沙&#39;,&#39;邵阳&#39;,&#39;常德&#39;,&#39;郴州&#39;,&#39;吉首&#39;,&#39;株洲&#39;,&#39;娄底&#39;,&#39;湘潭&#39;,&#39;益阳&#39;,&#39;永州&#39;,&#39;岳阳&#39;,&#39;衡阳&#39;,&#39;怀化&#39;,&#39;韶山&#39;,&#39;张家界&#39;);
$c[20] = array(&#39;杭州&#39;,&#39;湖州&#39;,&#39;金华&#39;,&#39;宁波&#39;,&#39;丽水&#39;,&#39;绍兴&#39;,&#39;雁荡山&#39;,&#39;衢州&#39;,&#39;嘉兴&#39;,&#39;台州&#39;,&#39;舟山&#39;,&#39;温州&#39;);
$c[21] = array(&#39;南昌&#39;,&#39;萍乡&#39;,&#39;九江&#39;,&#39;上饶&#39;,&#39;抚州&#39;,&#39;吉安&#39;,&#39;鹰潭&#39;,&#39;宜春&#39;,&#39;新余&#39;,&#39;景德镇&#39;,&#39;赣州&#39;);
$c[22] = array(&#39;福州&#39;,&#39;厦门&#39;,&#39;龙岩&#39;,&#39;南平&#39;,&#39;宁德&#39;,&#39;莆田&#39;,&#39;泉州&#39;,&#39;三明&#39;,&#39;漳州&#39;);
$c[23] = array(&#39;贵阳&#39;,&#39;安顺&#39;,&#39;赤水&#39;,&#39;遵义&#39;,&#39;铜仁&#39;,&#39;六盘水&#39;,&#39;毕节&#39;,&#39;凯里&#39;,&#39;都匀&#39;);
$c[24] = array(&#39;成都&#39;,&#39;泸州&#39;,&#39;内江&#39;,&#39;凉山&#39;,&#39;阿坝&#39;,&#39;巴中&#39;,&#39;广元&#39;,&#39;乐山&#39;,&#39;绵阳&#39;,&#39;德阳&#39;,&#39;攀枝花&#39;,&#39;雅安&#39;,&#39;宜宾&#39;,&#39;自贡&#39;,&#39;甘孜州&#39;,&#39;达州&#39;,&#39;资阳&#39;,&#39;广安&#39;,&#39;遂宁&#39;,&#39;眉山&#39;,&#39;南充&#39;);
$c[25] = array(&#39;广州&#39;,&#39;深圳&#39;,&#39;潮州&#39;,&#39;韶关&#39;,&#39;湛江&#39;,&#39;惠州&#39;,&#39;清远&#39;,&#39;东莞&#39;,&#39;江门&#39;,&#39;茂名&#39;,&#39;肇庆&#39;,&#39;汕尾&#39;,&#39;河源&#39;,&#39;揭阳&#39;,&#39;梅州&#39;,&#39;中山&#39;,&#39;德庆&#39;,&#39;阳江&#39;,&#39;云浮&#39;,&#39;珠海&#39;,&#39;汕头&#39;,&#39;佛山&#39;);
$c[26] = array(&#39;南宁&#39;,&#39;桂林&#39;,&#39;阳朔&#39;,&#39;柳州&#39;,&#39;梧州&#39;,&#39;玉林&#39;,&#39;桂平&#39;,&#39;贺州&#39;,&#39;钦州&#39;,&#39;贵港&#39;,&#39;防城港&#39;,&#39;百色&#39;,&#39;北海&#39;,&#39;河池&#39;,&#39;来宾&#39;,&#39;崇左&#39;);
$c[27] = array(&#39;昆明&#39;,&#39;保山&#39;,&#39;楚雄&#39;,&#39;德宏&#39;,&#39;红河&#39;,&#39;临沧&#39;,&#39;怒江&#39;,&#39;曲靖&#39;,&#39;思茅&#39;,&#39;文山&#39;,&#39;玉溪&#39;,&#39;昭通&#39;,&#39;丽江&#39;,&#39;大理&#39;);
$c[28] = array(&#39;海口&#39;,&#39;三亚&#39;,&#39;儋州&#39;,&#39;琼山&#39;,&#39;通什&#39;,&#39;文昌&#39;);
$c[29] = array(&#39;乌鲁木齐&#39;,&#39;阿勒泰&#39;,&#39;阿克苏&#39;,&#39;昌吉&#39;,&#39;哈密&#39;,&#39;和田&#39;,&#39;喀什&#39;,&#39;克拉玛依&#39;,&#39;石河子&#39;,&#39;塔城&#39;,&#39;库尔勒&#39;,&#39;吐鲁番&#39;,&#39;伊宁&#39;);
?>

returnpc.php:

<?php
require_once &#39;pc.php&#39;;  //包含省份数组和城市数组
$flag = $_GET[&#39;flag&#39;];   //用于区分是返回省份信息还是返回城市信息
$str="";
if($flag == p){   //如果参数flag的值为p,则返回省份信息
  for($i=0; $i<count($p); $i++){   //通过循环整理所有要追加到省份下拉列表的内容
    $str.="<option value=\"".iconv(&#39;gbk&#39;,&#39;utf-8&#39;,$p[$i])."\">".iconv(&#39;gbk&#39;,&#39;utf-8&#39;,$p[$i])."</option>";
  }
  echo $str;   //输出要返回的参数
}else{
  $index = array_search($_GET[&#39;p&#39;], $p);   //提取所选择省份的在省份数组中对应的键值
  for($j=0; $j<count($c[$index]); $j++){    //通过循环整理所有要追加到城市下拉列表的内容
    $str.= "<option value=\"".iconv(&#39;gbk&#39;,&#39;utf-8&#39;,$c[$index][$j])."\">".iconv(&#39;gbk&#39;,&#39;utf-8&#39;,$c[$index][$j])."</option>";
 }
 echo $str;    //输出要返回的参数
}
?>

Second running result

##

The above is the detailed content of How to implement cascade in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn