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]