Ajax implementation simple drop-down options

Source: Internet
Author: User

Basic is a fixed step! Operations primarily in JavaScript and PHP

1. There are only two select tags in the HTML code as follows:

<SelectID= "Province">    <option>Please select</option>  </Select>  <SelectID= "City">    <option>Please select</option>  </Select>

2. The steps to create an option in JavaScript and to perform an AJAX asynchronous request are as follows

<script>var xhr = GETXHR ();//The first time an AJAX asynchronous request is performed-Province window.onload = function () {Xhr.open ("get", "finaly.php?state=1"); Xhr.send (null); xhr.onreadystatechange = function () {if (xhr.readystate==4&&xhr.status==200) {var data = xhr.responsetext;//converts a string to an array of var provinces = Data.split (",");//iterates over the arrays for (Var i=0;i<provinces.length;i++) {// Create an OPTION element to add the ID to the province element on var option = document.createelement ("option"); var text = document.createTextNode ( Provinces[i]); Option.appendchild (text), var province = document.getElementById ("province");p Rovince.appendchild ( option);}}} The second time the Ajax asynchronous request is performed-city var Provinceele=document.getelementbyid ("province");p Rovinceele.onchange = function () {var town = document.getElementById ("City"), var opts = city.getelementsbytagname ("option"); for (Var z=opts.length-1;z>0;z--) {City.removechild (opts[z]);} if (province.value! = "Please select") {Xhr.open ("post", "finaly.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);}}}} function getxhr () {var xhr = null;if (window. XMLHttpRequest) {xhr = new XMLHttpRequest ();} ELSE{XHR = new ActiveXObject ("Microsoft.XMLHTTP");}  return XHR;} </script>

3, the PHP code is as follows: File name: finaly.php and JS: Xhr.open (method,url) method URL Docking!

<?php//receive the request data sent by the client-state$state = $_request[' state '];//determine the value of $state if ($state = = 1) {//Get the province Echo ' Shandong province, Liaoning Province, Jilin Province ';} else{//Get City $province = $_post[' province '];switch ($province) {case ' Shandong Province ': Echo ' Qingdao, Jinan, Weihai, Rizhao, Dezhou '; Break;case ' Liaoning Province ': Echo ' Shenyang, Dalian, Tieling, Dandong, Jinzhou '; Break;case ' Jilin Province ': Echo ' Changchun, Songyuan, Jilin, Tonghua, Siping '; >

The key is how to implement Ajax asynchronous interaction: Reference JS code. Can say the fixed steps

Ajax implementation simple drop-down options

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.