Ajax according to city name, automatically complete the corresponding city information _ajax related

Source: Internet
Author: User
Tags html page
I used to meet this kind of demand in my project. Users want to be able to directly enter the city's name, they can get the city's corresponding information. You used to generate a few JavaScript arrays when the page was open, and then contrast that. In doing so, if the data is more numerous, the user will obviously feel the response delay.

Now it's time to do it in an AJAX way, faster and with a better customer experience. Who wants us to be in the "Experience economy" era!

Here is a small example of doing, consisting of two pages.

The city.html page code is as follows:

<script> function Createrequestobject () {var ro; var browser = navigator.appname; if (browser = = "Microsoft Internet Explorer") {ro = new ActiveXObject ("Microsoft.XMLHTTP"); }else{ro = new XMLHttpRequest (); return ro; var http = createrequestobject (); function Sndreq (city) {http.open (' get ', ' zipcode.jsp?city= ' +city); Http.onreadystatechange = Handleresponse; Http.send (NULL); The function Handleresponse () {if (http.readystate = = 4) {var response = Http.responsetext; var update = new Array (); if (Response.indexof (' | '!=-1)) {update = Response.split (' | '); document.getElementById ("Tier"). Value = Update[0]; document.getElementById ("RMM"). Value = Update[1]; </script> <center> <p> Get CITY-appropriate information </p> <table align= "center" > <t R> <td> input City Pinyin:</td> <td><input type= "text" id= "Citycode" name= "Citycode" onblur= "Sndreq" (This . value); /></td> </tr> <tr> <td> level:</td> <td><input type= "text" id= "t Ier "name=" tier "/></td> </tr> <tr> <td> leader:</td> <td><input Type= "text" id= "RMM" name= "RMM"/></td> </tr> </table> </center>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


The zipcode.jsp code is as follows:
Copy Code code as follows:

<%@ page contenttype= "text/html;charset=gb2312"%>

<%@ page import= "java.util.*"%>
<%@ page import= "Java.sql.Connection"%>
<%@ page import= "Java.sql.PreparedStatement"%>
<%@ page import= "Java.sql.ResultSet"%>

<%@ page import= "Com.util.MyConnection"%>
<%
String City = Request.getparameter ("city");

if (city==null)
{
City= "";
}
if (!city.equals (""))
{
String sql= "Select Citrmm,cittier from tb_basecity where citname like '" +city+ "";
Connection conn = Myconnection.getconnection ();
PreparedStatement state= conn.preparestatement (SQL);
ResultSet rs = State.executequery ();

if (Rs.next ())
{
Out.println (rs.getstring ("CITRMM") + "|" +rs.getstring ("Cittier"));
}
Else
{
Out.println ("Unknown" + "|") + "Unknown");
}

Rs.close ();
State.close ();
Conn.close ();

}
Else
{
Out.println ("Unknown" + "|") + "Unknown");
}

%>

Ps:
After Http.send (), after the detection of http.readystate==4, the instructions loaded, when the Web page content has been loaded into the Http.responsebody
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.