Ajax achieves second-level urban linkage (2) and ajax achieves city Linkage

Source: Internet
Author: User

Ajax achieves second-level urban linkage (2) and ajax achieves city Linkage

In the previous article, only ajax requests were obtained and rendered for cities. Here, the provinces are also rendered using ajax requests.

1. HTML

<Select id = "province"> <option> select </option> </select> <select id = "city"> <option> select </option>/ select>

2. JS

/** Province information and city information are all from the server side ** first approach-based on the previous case ** to obtain province information, use an Ajax asynchronous request ** Based on province information, use Ajax asynchronous requests again ** the second approach-rethink ** one-time acquisition of provinces and cities * //. create the XMLHttpRequest object var xhr = getXhr (); // first thought-based on the previous case // 1. when the page is loaded, implement Ajax asynchronous request-province information window. onload = function () {// B. establish a connection-open ("get", "07_province.php"); xhr. open ("get", "07_province.php"); // c. send request-send (null) xhr. send (null); // d. receive data from the server xhr. onreadys Tatechange = function () {if (xhr. readyState = 4 & xhr. status = 200) {var data = xhr. responseText; // converts a string to an array var provinces = data. split (","); // traverse the array for (var I = 0; I <provinces. length; I ++) {// create the option element and add it to the var option = document. createElement ("option"); var text = document. createTextNode (provinces [I]); option. appendChild (text); var province = document. getElementById ("province"); province. AppendChild (option) ;}}}; // 2. when you select province information, implement Ajax asynchronous request-city information var province = document. getElementById ("province"); province. onchange = function () {// clear var city = document. getElementById ("city"); var opts = city. getElementsByTagName ("option"); for (var z = opts. length-1; z> 0; z --) {city. removeChild (opts [z]);} if (province. value! = "Select") {xhr. open ("post", "07_cities.php"); xhr. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); xhr. send ("province =" + province. value); xhr. onreadystatechange = function () {if (xhr. readyState = 4 & xhr. status = 200) {var data = xhr. responseText; var cities = data. split (","); for (var I = 0; I <cities. length; I ++) {var option = document. createElement ("option"); var text = document. createTextNode (cities [I]); option. appendChild (text); city. appendChild (option) ;}}}}; // defines the function getXhr () {var xhr = null for obtaining the Ajax core object; if (window. XMLHttpRequest) {xhr = new XMLHttpRequest ();} else {xhr = new ActiveXObject ("Microsoft. XMLHttp ");} return xhr ;}

3. province. php

<? Php echo 'shandong province, Liaoning Province, Jilin Province ';?>

Cities. pnp

<? Php // data used to process the second-level Association of client requests // 1. receive province information sent by the client $ province =$ _ POST ['province ']; // 2. determine the current province information and provide different city information switch ($ province) {case 'shandong province ': echo 'qingdao, Jinan, Weihai, Rizhao, Dezhou'; break; case 'liaoning province ': echo 'shenyang city, Dalian city, Tieling City, Dandong City, Jinzhou City'; break; case 'Jilin province ': echo 'changchun city, Songyuan City, Jilin City, Tonghua City, Siping City '; break;} // is the response string from the server?>

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.