Home >Backend Development >PHP Tutorial >Level 3 regional linkage based on database, php, ajax
In the past, every time I wanted to use regional linkage, I basically used multi-level linkage using js written by others. However, I found that the biggest shortcoming was that the regional information was too old and it was inconvenient to update.
Today, I searched the latest national and regional information data in 2015 on the Internet, and I plan to use the database to cooperate with ajax for linkage. The background can also easily update the data
Code 1 (jquery code, responsible for the front part of ajax data linkage):
<code><span><<span>div</span><span>class</span>=<span>"form-group"</span>></span><span><<span>label</span><span>for</span>=<span>"province"</span>></span>省份<span></<span>label</span>></span><span><<span>select</span><span>class</span>=<span>"form-control"</span><span>id</span>=<span>"province"</span><span>name</span>=<span>"province"</span>></span><span><<span>option</span><span>value</span> =<span>""</span> ></span><span></<span>option</span>></span> //volist 是thinkphp的标签,这里我用thinkphp框架写的代码,自动载入省级地区信息 <span><<span>volist</span><span>name</span>=<span>"province"</span><span>id</span>=<span>"vo"</span>></span><span><<span>option</span><span>value</span> =<span>"{$vo.class_id}"</span> ></span>{$vo.class_name}<span></<span>option</span>></span><span></<span>volist</span>></span><span></<span>select</span>></span><span><<span>label</span><span>for</span>=<span>"city"</span>></span>城市<span></<span>label</span>></span><span><<span>select</span><span>class</span>=<span>"form-control"</span><span>id</span>=<span>"city"</span><span>name</span>=<span>"city"</span>></span><span></<span>select</span>></span><span><<span>label</span><span>for</span>=<span>"county"</span>></span>县区<span></<span>label</span>></span><span><<span>select</span><span>class</span>=<span>"form-control"</span><span>id</span>=<span>"county"</span><span>name</span>=<span>"county"</span>></span><span></<span>select</span>></span><span></<span>div</span>></span><span><<span>script</span>></span><span> $(document).ready(<span><span>function</span><span>()</span>{</span><span>//省数据变动</span> $(<span>"#province"</span>).change(<span><span>function</span><span>()</span>{</span> $.post(<span>'/Home/Creat/ajax'</span>, {province:$(<span>"#province"</span>).val()}, <span><span>function</span><span>(data)</span>{</span><span>//alert('成功post回数据');</span><span>if</span>(data.info == <span>'ok'</span>) { <span>//返回ok,将市下拉框赋值</span><span>//alert(data.content);</span> col_clear(<span>'city'</span>); col_add(<span>'city'</span>,data.content); $(<span>"#city"</span>).change(); } <span>else</span> { alert(data.info); } }, <span>'json'</span>).error(<span><span>function</span><span>()</span>{</span> alert(<span>"网络连接错误,请稍后再试"</span>); }); }); <span>//市数据变动</span> $(<span>"#city"</span>).change(<span><span>function</span><span>()</span>{</span> $.post(<span>'/Home/Creat/ajax'</span>, {city:$(<span>"#city"</span>).val()}, <span><span>function</span><span>(data)</span>{</span><span>if</span>(data.info == <span>'ok'</span>) { col_clear(<span>'county'</span>); col_add(<span>'county'</span>,data.content); } <span>else</span> { alert(data.info); } }, <span>'json'</span>).error(<span><span>function</span><span>()</span>{</span> alert(<span>"网络连接错误,请稍后再试"</span>); }); }); }); <span>//select操作方法</span><span>// 添加 </span><span><span>function</span><span>col_add</span><span>(id,data)</span> {</span><span>var</span> selObj = $(<span>"#"</span>+id); <span>for</span> (<span>var</span> i=<span>0</span>;i<data.length;i++) { selObj.append(<span>"<option value='"</span>+data[i].class_id+<span>"'>"</span>+data[i].class_name+<span>"</option>"</span>); } } <span>// 删除 </span><span><span>function</span><span>col_delete</span><span>()</span> {</span><span>var</span> selOpt = $(<span>"#mySelect option:selected"</span>); selOpt.remove(); } <span>// 清空 </span><span><span>function</span><span>col_clear</span><span>(id)</span> {</span><span>var</span> selOpt = $(<span>"#"</span>+id+<span>" option"</span>); selOpt.remove(); } <span></<span>script</span>></span></code>
Code 2 (php back-end data judgment interactive code, written based on thinkphp. If it is other frameworks, the principle is the same, the code will have some slight differences):
<code><span>public</span><span><span>function</span><span>ajax</span><span>()</span>{</span><span>$city</span> = M(<span>'city'</span>); <span>if</span>(!IS_AJAX){ <span>$this</span>->ajaxReturn(<span>array</span>( <span>'info'</span> => <span>'非法的请求方式'</span> )); } <span>//如果提交过来的是省份改变</span><span>if</span>(I(<span>'post.province'</span>,<span>0</span>,<span>'intval'</span>)){ <span>$filter</span> = <span>array</span>( <span>'class_parent_id'</span> => I(<span>'post.province'</span>,<span>0</span>,<span>'intval'</span>), <span>'class_type'</span> => <span>2</span> ); <span>$data</span>[<span>'content'</span>] = <span>$city</span>->where(<span>$filter</span>)->select(); <span>$data</span>[<span>'info'</span>] = <span>'ok'</span>; } <span>//如果提交过来的是城市改变</span><span>if</span>(I(<span>'post.city'</span>,<span>0</span>,<span>'intval'</span>)){ <span>$filter</span> = <span>array</span>( <span>'class_parent_id'</span> => I(<span>'post.city'</span>,<span>0</span>,<span>'intval'</span>), <span>'class_type'</span> => <span>3</span> ); <span>$data</span>[<span>'content'</span>] = <span>$city</span>->where(<span>$filter</span>)->select(); <span>$data</span>[<span>'info'</span>] = <span>'ok'</span>; } <span>$this</span>->ajaxReturn(<span>$data</span>); }</code>
The last is the database, contribute your own database, according to The regional information published by the state in January 2015 is the latest.
sql database download
The above introduces the level 3 regional linkage based on database, php, and ajax, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.