Using JSON to implement the provincial and municipal level two drop-down cascading menu [original]

Source: Internet
Author: User

This article for Bo Master Original, reproduced please specify.

First look at the implementation:

Of course, to complete this experiment, MySQL must be connected with the database, here choose Navicat for MySQL this software, it and MySQL fit is very high, configuration environment not much to repeat, Note Modify the PHP configuration file php.ini file, will mysql.dll the front of the ";" Get rid of, or will always report the database connection function error.

Configure the above to enter the code stage, gossip not much to say, directly on the code:

2-7-3.php:
<! DOCTYPE html> $(function(){ $("#province"). Change (function(){ varPID = $ (this).Val (); vardata = { "pid":PID}; Console.Log(data); $. Post ("getcity.php", data,function(response) {Console.Log(response); //json.stringify (RES); Console.log (Json_decode (response)); document.write (' AFDADF ');$ ("#city").HTML (response); }) }) })</script>PHPmysql_connect("localhost", "root", "123456"); mysql_select_db("Lession"); mysql_query("Set Names UTF8"); $sql= "SELECT * from Province"; $res=mysql_query($sql); while($row=Mysql_fetch_assoc($res)) { Echo"<option value=\" {$row[' ID ']} \ ">{$row[' Name ']} </option> "; } ?> </select> <select id= "City" > <option> Select cities </option></select></body></ Html>

Then the backend server-side code:

getcity.php:
<?PHPif(isset($_post["pid"]) &&$_post["pid"]) { $pid=$_post[' PID ']; Var_dump($pid);}//$pid = $_post[' pid '];$link=mysql_connect("localhost", "root", "123456");//a single database can omit variable return valuesmysql_select_db("Lession",$link);mysql_query("Set Names UTF8");$sql= "SELECT *" from the city where province_id={$pid}";$res=mysql_query($sql);$citys=Array();if($res) { while($row=Mysql_fetch_assoc($res)) { Echo"<option value= ' {$row[' ID ']} ' >{$row[' Name ']} </option> "; //$citys [] = array ("id" = = $row [' id '], ' name ' = = $row [' name ']); }//Echo Json_encode ($citys);}

By the right, forgot to explain: the previous database named: lession

Write to this moment found that there is no attachment this function, originally also want to upload this database file, it is awkward ah, but it doesn't matter, Bo Master is Baidu Cloud people (instant tease than the nature and exposed), has been uploaded to Http://pan.baidu.com/s/1mivSSRY, Interested to do this experiment of the vast number of Bo friends can try, the big God do not spray, Bo Master is quasi-junior students, hey.

Using JSON to implement the provincial and municipal level two drop-down cascading menu [original]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.