This php ajax application instance uses ajax + php to call the urban second-level linkage menu, so that we can update the menu in real time based on the database content.
The ajax application example of this php tutorial is an ajax + php call of the second-level urban linkage menu, so that we can update the menu in real time according to the database tutorial content.
<Html>
<Head>
<Title> ajax + php application instance with no additional level-2 linkage menu </title>
<Script language = "webpage effect">
Var xmlhttp = null;
Function getxmlhttprequest ()
{
Var xmlhttp = null;
Try
{
Xmlhttp = new xmlhttprequest ();
}
Catch (e)
{
Try
{
Xmlhttp = new activexobject ("msxml2.xmlhttp ");
}
Catch (e)
{
Try
{
Xmlhttp = new activexobject ("microsoft. xmlhttp ");
}
Catch (e)
{
Xmlhttp = false;
}
}
}
Return xmlhttp;
}
Function sendrequest ()
{
Var prov_name = document. getelementbyid ("province"). value;
If (prov_name = null) | (prov_name = ""))
Return;
Xmlhttp = getxmlhttprequest ();
If (xmlhttp = null)
{
Alert ("the browser does not support xmlhttprequest! ");
Return;
}
Var url = "www. bKjia. c0m. php ";
Url = url + "? Prov = "+ prov_name;
Xmlhttp. open ("get", url, true );
Xmlhttp. onreadystatechange = updatepage;
Xmlhttp. send (null );
}
Function updatepage ()
{
If (xmlhttp. readystate = 4 & xmlhttp. status = 200)
{
Var response = xmlhttp. responsetext;
Document. getelementbyid ("city"). innerhtml = response;
}
}
</Script>
<Head>
<Body>
<H3> select a province (autonomous region):
<Form action = "www. bKjia. c0m. php">
<Div>
<Select id = "province" onchange = "sendrequest ()">
<Option value = ""> select a province (autonomous region) </option>
<Option value = "ah"> Anhui </option>
<Option value = "fj"> Fujian </option>
<Option value = "gs"> Gansu </option>
<Option value = "gd"> Guangdong </option>
<Option value = "gx"> Guangxi </option>
<Option value = "gz"> Guizhou </option>
<Option value = "hn"> Hainan </option>
<Option value = "hb"> Hebei </option>
<Option value = "hh"> Henan </option>
<Option value = "hl"> Heilongjiang </option>
</Select>
</Div>
</Form>
<Div id = "city">
</Div>
</Body>
</Html>
Www. bKjia. c0m. php code
<? Php
$ City_arr = array (
"Ah" => "Hefei ",
"Fj" => "Fuzhou ",
"Gs" => "Lanzhou ",
"Gd" => "Guangzhou ",
"Gx" => "Nanning ",
"Gz" => "Guiyang ",
"Hn" => "Haikou ",
"Hb" => "Shijiazhuang ",
"Hh" => "Zhengzhou ",
"Hl" => "Harbin"
);
If (empty ($ _ get ['prov'])
{
Echo iconv ("gb2312", "UTF-8", '<font color = "red"> you have not selected a province (autonomous region) </font> ');
}
Else
{
$ Prov = $ _ get ['prov'];
$ City = $ city_arr [$ prov];
Echo iconv ("gb2312", "UTF-8", 'provincial (Autonomous Region) Capital: '. $ city );
}
?>