Example of three-level linkage function of provinces and municipalities implemented by thinkPHP

不言
Release: 2023-03-30 09:26:02
Original
1939 people have browsed it

This article mainly introduces the three-level linkage function of provinces and municipalities implemented by thinkPHP, and analyzes the detailed steps and related operating techniques of thinkPHP to implement the three-level linkage function of provinces and municipalities. Friends in need can refer to the following

The example of this article describes the three-level linkage function of provinces and municipalities implemented by thinkPHP. Share it with everyone for your reference, the details are as follows:

One table realizes three-level linkage of provinces and municipalities [3409 pieces of data]

1. PHP code:

public function index(){
  $province = M('Tree')->where ( array('pid'=>1) )->select ();
  $this->assign('province',$province);
  $this->display();
}
public function getRegion(){
  $Region=M("Tree");
  $map['pid']=$_REQUEST["pid"];
  $map['type']=$_REQUEST["type"];
  $list=$Region->where($map)->select();
  echo json_encode($list);
}
Copy after login

2. HTML code:

<select name="province" id="province" onchange="loadRegion(&#39;province&#39;,2,&#39;city&#39;,&#39;{:U(&#39;Index/getRegion&#39;)}&#39;);">
  <option value="0" selected>省份/直辖市</option><volist name="province" id="vo">
  <option value="{$vo.id}" >{$vo.name}</option></volist>
</select>
<select name="city" id="city" onchange="loadRegion(&#39;city&#39;,3,&#39;town&#39;,&#39;{:U(&#39;Index/getRegion&#39;)}&#39;);">
 <option value="0">市/县</option>
</select>
<select name="town" id="town">
 <option value="0">镇/区</option>
</select>
Copy after login

3. JavaScript code:

function loadRegion(sel,type_id,selName,url){
  jQuery("#"+selName+" option").each(function(){
    jQuery(this).remove();
  });
  jQuery("<option value=0>请选择</option>").appendTo(jQuery("#"+selName));
  if(jQuery("#"+sel).val()==0){
    return;
  }
  jQuery.getJSON(url,{pid:jQuery("#"+sel).val(),type:type_id},
    function(data){
      if(data){
        jQuery.each(data,function(idx,item){
          jQuery("<option value="+item.id+">"+item.name+"</option>").appendTo(jQuery("#"+selName));
        });
      }else{
        jQuery("<option value=&#39;0&#39;>请选择</option>").appendTo(jQuery("#"+selName));
      }
    }
  );
}
Copy after login

4. SQL code:

DROP TABLE IF EXISTS `tp_tree`;
CREATE TABLE `tp_tree` (
 `id` int(5) unsigned NOT NULL AUTO_INCREMENT,
 `pid` int(5) unsigned NOT NULL DEFAULT &#39;0&#39;,
 `name` varchar(120) DEFAULT NULL,
 `type` tinyint(1) DEFAULT &#39;2&#39;,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3410 DEFAULT CHARSET=utf8;
Copy after login

5. TP_tree.sql file.

Related recommendations:

How to implement asynchronous transmission technology using the built-in ThinkAjax of ThinkPHP

Using ajax reception in ThinkPHP json data method

The above is the detailed content of Example of three-level linkage function of provinces and municipalities implemented by thinkPHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template