Note:
1. to send data using POST, the line 2 function (also called ajax to send data: ajaxCall) must add the following sentence: xmlObject. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
Then use xmlObject. send (data); To send
Note the following for line 2.3 functions:
1. Disable Cache (recommended, not required): header ("cache-Control: no-Cache ");
2. the header ("Content-Type: text/XML; charset = gb2312") must be added to the xml data format; // XML must be written here
3. If you use MYSQL installed in the WAMP5 integrated environment, you must add:
$ Charset = "gb2312 ";
Mysql_query ("SET character_set_connection = $ charset, character_set_results = $ charset, character_set_client = binary"); // This sentence is required to solve Chinese garbled encryption problem s
Otherwise, garbled encryption will occur. Today I am wasting a long time here. I am using the default database installed in ECSHOP GBK.
4. If XML is used to accept data, the callback function must be divided into IE and non-IE for processing. Otherwise, one party will always be unable to accept XML data.
The processing code is as follows:
Copy codeThe Code is as follows: function getXMLData (tagName) // retrieves XML data, which is processed by IE and non-IE.
{
Var info;
If (window. ActiveXObject) // Method for retrieving XML files from IE
{
Var doc = new ActiveXObject ("MSxml2.DOMDocument ");
Doc. loadXML (xmlObject. responseText );
Info = doc. getElementsByTagName (tagName );
}
Else // ----------------------------- non-IE Method for retrieving XML files
{
Info = xmlObject. responseXML. getElementsByTagName (tagName );
}
Return info;
}
The following is a provincial/municipal Linkage test
The Code is as follows:
Index. php
Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> provincial/municipal Linkage test </title>
<Style type = "text/css">
Select {
Width: 100px;
}
</Style>
<Script type = "text/javascript">
Var thisId = ""; // ID of selectI for the current operation
Var xmlObject; // global variable of the ajax object,
Function getAjaxObject () // AJAX line 1, returns an AJAX object Engine
{
Var xmlObject;
If (window. ActiveXObject)
{
XmlObject = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Else
{
XmlObject = new XMLHttpRequest ();
}
Return xmlObject;
}
Function ajaxCall (id) // ajax Line 2. The post parameter is used here.
{
XmlObject = new getAjaxObject ();
If (xmlObject)
{
Var url = "chuli. php ";
Var data = "id =" + id;
XmlObject. open ("post", url, true );
XmlObject. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
XmlObject. onreadystatechange = repayFuncion;
XmlObject. send (data );
}
}
Function repayFuncion () // ajax line 4. xml is used to accept data, and xmldom programming is also involved.
{
If (xmlObject. readyState = 4 & xmlObject. status = 200)
{
Var info = getXMLData ("res"); // get XML data
$ (ThisId). length = 0; // clear the option node in the select statement.
For (I = 0; I <info. length; I ++)
{
Var optionId = info [I]. childNodes [0]. childNodes [0]. nodeValue;
Var optionValue = info [I]. childNodes [1]. childNodes [0]. nodeValue;
Var optionNode = document. createElement ('option ');
OptionNode. value = optionId;
OptionNode. innerText = optionValue;
$ (ThisId). appendChild (optionNode );
}
}
}
Function getXMLData (tagName) // retrieves XML data, which is processed by IE and non-IE.
{
Var info;
If (window. ActiveXObject) // Method for retrieving XML files from IE
{
Var doc = new ActiveXObject ("MSxml2.DOMDocument ");
Doc. loadXML (xmlObject. responseText );
Info = doc. getElementsByTagName (tagName );
}
Else // ----------------------------- non-IE Method for retrieving XML files
{
Info = xmlObject. responseXML. getElementsByTagName (tagName );
}
Return info;
}
Function $ (id) // common functions, using ID to retrieve objects
{
Return document. getElementById (id );
}
Function getProvice () // obtain Province
{
ThisId = "Province ";
Var id = '1 ';
AjaxCall (id );
}
Function getCity () // get the city
{
ThisId = "City ";
$ ("County"). length = 0;
Var id = $ ("Province"). value;
AjaxCall (id );
}
Function getCounty () // obtain the county seat
{
ThisId = "County ";
Var id = $ ("City"). value;
If ($ ("City"). length)
{
AjaxCall (id );
}
}
Window. onlaod = getProvice (); // load the page to Province
</Script>
</Head>
<Body>
<Form action = "javascript: void (0)" method = "post">
<Label for = "username"> User Name: </label> <input type = "text" name = "username" id = "username" width = "60px"/> <br/>
<Label for = "psd"> password: </label> <input type = "password" name = "psd" id = "psd" width = "80px"/> </br>
<Label for = "psd"> address: </label>
<Select id = "Province" onclick = "getCity ()">
</Select>
<Select id = "City" onclick = "getCounty ()">
</Select>
<Select id = "County" name = "xian">
</Select>
<Input type = "submit" value = "submit"/>
</Form>
</Body>
</Html>
Chuli. php
Copy codeThe Code is as follows: <? Php
// Line 3
Header ("Cache-Control: no-cache ");
Header ("Content-Type: text/xml; charset = gb2312"); // write XML
Require ("function. php ");
$ Id = $ _ POST ['id'];
File_put_contents ("my1.txt", $ act. "------". $ ziduan );
$ Result = getresultById ($ id );
$ Info = "<mes> ";
Foreach ($ result as $ row)
{
$ Info. = "<res> ";
$ Info. = "<id>". $ row ['region _ id']. "</id> ";
$ Info. = "<name>". $ row ['region _ name']. "</name> ";
$ Info. = "</res> ";
}
$ Info. = "</mes> ";
Echo $ info;
?>
3. database functions
Function. php
Copy codeThe Code is as follows: <? Php
Function getresultById ($ id)
{
$ Con = mysql_connect ("localhost", "root ","");
If ($ con)
{
$ Charset = "gb2312 ";
Mysql_query ("SET character_set_connection = $ charset, character_set_results = $ charset, character_set_client = binary"); // This sentence is required to solve Chinese garbled encryption problem s
Mysql_select_db ("ajax", $ con );
$ SQL = "select * from ecs_region where parent_id = '$ id '";
$ Res = mysql_query ($ SQL );
$ Arr = array ();
While ($ row = mysql_fetch_assoc ($ res ))
{
$ Arr [] = $ row;
}
Return $ arr;
}
Return false;
}