Note the point:
1. Send data with post, in line Line 2 function (also AJAX send data function: Ajaxcall) must add a sentence: Xmlobject.setrequestheader ("Content-type", "application/ X-www-form-urlencoded ");
Then use Xmlobject.send (data);
No. 2.3 Line function to note:
1. Disable caching (recommended, not required): header ("Cache-control:no-cache");
2. Use XML data format must add: Header ("Content-type:text/xml; charset=gb2312 ");/here to write XML
3. If MySQL is installed using the WAMP5 integrated environment, it must be added when querying the database:
$charset = "gb2312";
mysql_query ("SET character_set_connection= $charset, character_set_results= $charset, character_set_client=binary") ; This sentence is necessary to solve the Chinese garbled encryption problem s
Otherwise it will be garbled encryption, today I was wasting a long time here, I was using ecshop GBK version of the default installed database
4. If you use XML to accept data, the callback function must be handled by IE and non ie, otherwise there will always be one party that cannot marry XML data
The processing code is as follows:
Copy Code code as follows:
function Getxmldata (tagName)//Get XML data, divide IE and non ie processing
{
var info;
if (window. ActiveXObject)//ie Retrieve XML file method
{
var doc = new ActiveXObject ("Msxml2.domdocument");
Doc.loadxml (Xmlobject.responsetext);
info = doc.getelementsbytagname (tagName);
}
else//---------------------------non IE retrieve XML file method
{
info = xmlObject.responseXML.getElementsByTagName (tagName);
}
return info;
}
Here's what I did. A provincial linkage test
The code is as follows:
index.php
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Convenience linkage Test </title>
<style type= "Text/css" >
select{
width:100px;
}
</style>
<script type= "Text/javascript" >
var thisid = ""; Current operation of the Selecti D
var xmlobject; Ajax Object Global variables,
function Getajaxobject ()//ajax line 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 Line 2, which uses post pass parameters
{
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 Line 4, which uses XML to accept data, and here also involves XMLDOM programming
{
if (xmlobject.readystate==4 && xmlobject.status==200)
{
var info = getxmldata ("res");//Get XML data
$ (thisid). length = 0;//Clear option node in select
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)//Get XML data, divide IE and non ie processing
{
var info;
if (window. ActiveXObject)//ie Retrieve XML file method
{
var doc = new ActiveXObject ("Msxml2.domdocument");
Doc.loadxml (Xmlobject.responsetext);
info = doc.getelementsbytagname (tagName);
}
else//---------------------------non IE retrieve XML file method
{
info = xmlObject.responseXML.getElementsByTagName (tagName);
}
return info;
}
function $ (ID)//Common functions, object by ID
{
return document.getElementById (ID);
}
function Getprovice ()//Get Province
{
Thisid = "province";
var id = ' 1 ';
Ajaxcall (ID);
}
function getcity ()//Get City
{
Thisid = "City";
$ ("County"). length = 0;
var id = $ ("province"). Value;
Ajaxcall (ID);
}
function Getcounty ()//Get County
{
Thisid = "County";
var id = $ ("city"). Value;
if ($ ("City"). Length)
{
Ajaxcall (ID);
}
}
Window.onlaod = Getprovice ();//page starts loading into the province
</script>
<body>
<form action= "javascript:void (0)" method= "POST" >
<label for= "username" > Username:</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= "submitted"/>
</form>
</body>
chuli.php
Copy Code code as follows:
<?php
Line Line 3.
Header ("Cache-control:no-cache");
Header ("Content-type:text/xml; charset=gb2312 ");/here to 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 Code code 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 necessary to solve the 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;
}