How Ajax realizes urban secondary linkage
This time I will show you how Ajax realizes the second-level linkage of cities. What are the precautions for Ajax to realize second-level linkage of cities. The following is a practical case, let’s take a look.
1, html
<select id="province"> <option>请选择</option> <option>山东省</option> <option>辽宁省</option> <option>吉林省</option> </select> <select id="city"> <option>请选择</option> </select>
2, javascript
<script> /* * 需要思考哪些事情? * * 在什么时候执行Ajax的异步请求? * * 当用户选择具体的省份信息时 */ // 1. 为id为province元素绑定onchange事件 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(provinceEle.value != "请选择"){ // 2. 执行Ajax异步请求 var xhr = getXhr(); xhr.open("post","06.php"); xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xhr.send("province="+provinceEle.value); xhr.onreadystatechange = function(){ if(xhr.readyState==4&&xhr.status==200){ // 接收服务器端的数据内容 var data = xhr.responseText; // data是字符串,转换为数组 var cities = data.split(","); for(var i=0;i<cities.length;i++){ var option = document.createElement("option"); var textNode = document.createTextNode(cities[i]); option.appendChild(textNode); city.appendChild(option); } } } } }; // 定义获取ajax核心对象的函数XMLHttpRequest对象的函数 function getXhr(){ var xhr = null; if(window.XMLHttpRequest){ xhr = new XMLHttpRequest(); }else{ xhr = new ActiveXObject("Microsoft.XMLHttp"); } return xhr; } </script>
3, 06.php
<?php // 用于处理客户端请求二级联动的数据 // 1. 接收客户端发送的省份信息 $province = $_POST['province']; // 2. 判断当前的省份信息,提供不同的城市信息 switch ($province){ case '山东省': echo '青岛市,济南市,威海市,日照市,德州市'; break; case '辽宁省': echo '沈阳市,大连市,铁岭市,丹东市,锦州市'; break; case '吉林省': echo '长春市,松原市,吉林市,通化市,四平市'; break; } // 服务器端响应的是字符串 ?>
I believe you will read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Using Blod to make ajax progress bar download
Code caused by multiple asynchronous Ajax requests How to solve nesting
The above is the detailed content of How Ajax realizes urban secondary linkage. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

There are many treasure chests in Mingchao Jinzhou City. Many players want to know where the treasure chests are. The editor has marked the locations of all the treasure chests. Players can just follow the marked numbers to find them one by one. Let’s take a look at this Mingchao article for details. The latest collection of all treasure chests in Jinzhou City. Mingchao Strategy Collection Mingchao Jinzhou City Treasure Box Collection Location 1: Jinzhou City (1-25) Twenty-five treasure boxes, five voice boxes Location 2: Jinzhou City (26-41) Fifteen treasure boxes, three A voice box, two flying hunters

If your Bluetooth speakers or headphones on your Windows 11/10 computer are emitting a constant beeping sound, this may be ruining your music enjoyment. In this article, you will find ways to solve this problem so that you can eliminate this annoying situation. Bluetooth speakers or headphones keep beeping on Windows 11/10 If your Bluetooth headphones or speakers keep beeping on Windows 11/10, use the following suggestions to resolve the issue. Preliminary Steps Run the Audio Troubleshooter Update the firmware of your Bluetooth device Reinstall the required drivers Reset your Bluetooth device Your device may be faulty We have explained all these fixes in detail below. 1] Preliminary steps First, perform some preparatory steps. If these

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.

How to use Ajax functions to achieve asynchronous data interaction With the development of the Internet and Web technology, data interaction between the front end and the back end has become very important. Traditional data interaction methods, such as page refresh and form submission, can no longer meet user needs. Ajax (Asynchronous JavaScript and XML) has become an important tool for asynchronous data interaction. Ajax enables the web to use JavaScript and the XMLHttpRequest object
