PHP + JS second-level linkage

Source: Internet
Author: User

<?
Require dirname (_ file _). '/include/common. Inc. php ';
Global $ dB;
$ Area = $ db-> select ("select * From cms_area where parentid = 0 ");
?>
<Form ID = "myform" name = "myform" Action = "" method = "Post">
<Select name = "areaid" id = "areaid" onchange = "getarea2 (document. myform. areaid. Options [document. myform. areaid. selectedindex]. Value);">
<Option value = "-1">-select-</option>
<? Foreach ($ area as $ R) {?>
<Option value = "<? = $ R [areaid]?> "> <? = $ R ['name']?> </Option>
<? }?>
</SELECT>
<Select name = "areaid2" id = "areaid2">
<Option value = "-1">-business area-</option>
</SELECT>
</Form>
<SCRIPT>
Function getarea2 (PID)
{

VaR area = new array ();
VaR num = 0;
VaR DATA = new array ();
<? PHP
$ Area = getarea ();
$ Rownum = count ($ area );
For ($ I = 0; $ I <$ rownum; $ I ++ ){
Echo "data [". $ I. "] = new array ('". $ area [$ I] ["name"]. "',". $ area [$ I] ["areaid"]. ",". $ area [$ I] ["parentid"]. ") \ n"; // create a two-dimensional array with array _ option elements. The first column is the text of the city, and the second column is the city id, the third column is the parent ID of the city.
}

?>

Document. getelementbyid ('areaid2'). Length = 0;

For (I = 0; I <data. length; I ++)
{
If (data [I] [2] = PID)
{
Document. getelementbyid ('areaid2 '). options [document. getelementbyid ('areaid2 '). length] = New Option (data [I] [0], data [I] [1]);
}
}

}
</SCRIPT>
<?
Function getarea ()
{
Global $ dB;

$ Area = $ db-> select ("select * From cms_area where parentid in (select areaid from cms_area where parentid = 0 )");
Return $ area;
}
?>

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.