Ajax implementation Simple dropdown option effect "recommended" _ajax related

Source: Internet
Author: User
Tags php code

Basically are fixed steps! Operations primarily in JavaScript and PHP

1, there are only two select tags in the HTML code:

<select id= "province" >
  <option> Please select </option>
 </select>
 <select id= "City" >
  <option> Please select </option>
 </select>

2. Creating options in JavaScript and performing Ajax asynchronous requests are the following steps

<script> var xhr = getxhr (); 
    First time Ajax asynchronous request-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.response 
        Text; 
        Converts a string to an array of var provinces = Data.split (",");  Traversal array for (Var i=0;i<provinces.length;i++) {//create option element to add to the Id province element var option = 
          Document.createelement ("option"); 
          var text = document.createTextNode (provinces[i]); 
          Option.appendchild (text); 
          var province = document.getElementById ("province"); 
        Province.appendchild (option); 
  The second execution of the Ajax asynchronous request-the city var Provinceele=document.getelementbyid ("province"); 
    Provinceele.onchange = function () {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!= "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.re 
          Sponsetext; 
          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, PHP code as follows: File name: finaly.php and JS: Xhr.open (Method,url) method of the URL docking!

<?php 
  //Receiving request data sent by client-state 
  $state = $_request[' state ']; 
  To determine the value of $state 
  if ($state = = 1) {//Get the province 
    Echo ' Shandong province, Liaoning Province, Jilin Province '; 
  } else{//get the 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 '; 
        break; 
    } 
? > 

The key is how to implement the Ajax asynchronous interaction: Refer to the JS code. Can be said to the fixed steps.

The above Ajax implementation of simple Drop-down option effect "recommendation" is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.