Ajax achieves City Interaction

Source: Internet
Author: User

The remaining parts have not been written, and the subsequent parts can be completed by yourself. Database connection is not performed, and you need to connect to the database on your own.

ReferenceCode:

Showcity.html

<! Doctype HTML public "  -// W3C // dtd html 4.01 transitional // en  " > <HTML>  "  Content-Type  " Content = "  Text/html; charset = UTF-8  " > <! -- <LINK rel = " Stylesheet  " Type = "  Text/CSS  " Href = "  Styles.css  " > --> <SCRIPT type = "  Text/JavaScript  " > //  Create an Ajax Engine  Function getxmlhttprequest (){  VaR XMLHTTP;  If  (Window. activexobject) {XMLHTTP = New Activexobject ( "  Microsoft. XMLHTTP  "  );  //  Window. Alert ("ie "); } Else  {XMLHTTP = New  XMLHttpRequest ();} Return  XMLHTTP ;}  //  Sendrequest      VaR  XMLHttpRequest; function sendrequest (){  //  Get the XMLHTTPRequest object XMLHttpRequest = Getxmlhttprequest ();  VaR Url = "  /Ajax/citylist. php  "  ; VaR Data = "  Province =  " + $ ( '  Sheng  '  ). Value;  //  Window. Alert (data ); XMLHttpRequest. Open ( "  Post  " , URL, True  );  // This sentence must be added for post submission. XMLHttpRequest. setRequestHeader ( "  Content-Type  " , "  Application/X-WWW-form-urlencoded  "  );  //  Callback Function XMLHttpRequest. onreadystatechange = Function chuli (){  If (XMLHttpRequest. readystate = 4 ){  If (XMLHttpRequest. Status = "  200  "  ){  //  Retrieve results                      VaR Citys = XMLHttpRequest. responsexml. getelementsbytagname ( "  City  "  ); $ (  '  City ' ). Length = 0  ;  VaR Myoption = Document. createelement ( "  Option  "  ); Myoption. innertext = "  -- City --  "  ; $ (  '  City  ' ). Appendchild (myoption );  //  Window. Alert (Citys. Length );  //  Traversal                      For ( VaR I = 0 ; I <Citys. length; I ++ ){  //  Window. Alert (citys [I]. childnodes [0]. nodevalue );                          VaR City_name = citys [I]. childnodes [ 0 ]. Nodevalue;  //  Create new elements                          VaR Myoption = Document. createelement ( "  Option  "  ); Myoption. Value = City_name; myoption. innertext = City_name; $ (  "  City  " ). Appendchild (myoption );}}}}  //  Send data  XMLHttpRequest. Send (data);} function $ (ID ){  Return  Document. getelementbyid (ID );} </SCRIPT>  Select Id = "  Sheng  " Onchange = "  Sendrequest (); " > <Option value = "" > --- Save --- </option> <option value = "  Hunan  " > Hunan </option> <option value = "  Jiangsu  " > Jiangsu </option> </ Select > < Select Id = "  City  " > <Option value ="" > -- City -- </option> </ Select > < Select Id = "  County  " > <Option value = "" > -- County -- </option> </ Select > </Body> 

Citylist. php

<? PHP  //  The first speech tells the browser that the returned data is in XML format. Header ( " Content-Type: text/XML; charset = UTF-8  "  );  //  Tell the browser not to cache data Header ( "  Cache-control: No-Cache  "  );  //  Receive $ Province = $ _ post [ '  Province  '  ];  // Debugging File_put_contents ( "  D:/mylog. Log  " , $ Province. "  \ R \ n  "  , File_append); $ result = ""  ;  If ($ Province = "  Hunan  "  ) {$ Result ="  <Province> <city> Changsha </city> <city> Yueyang </city> <city> Hengyang </city> </province>  "  ;}  Else   If ($ Province = "  Jiangsu  "  ) {$ Result = "  <Province> <city> Nanjing </city> <city> Yancheng </city> <city> Suzhou </city> </province>  " ;} Echo $ result; ?>

 

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.