Three-level linkage menu for mysql php without refreshing

Source: Internet
Author: User
The code is as follows: Copy code

<Script language = "JavaScript" type = "text/webpage effect">
Function removeOptions (selectObj ){
If (typeof selectObj! = 'Object '){
SelectObj = document. getElementById (selectObj );
} // Count the original options
Var len = selectObj. options. length;
For (var I = 0; I <len; I ++) {// remove the current option
SelectObj. options [0] = null;
    }
}
Function setSelectOption (selectObj, optionList, firstOption, selected ){
If (typeof selectObj! = 'Object '){
SelectObj = document. getElementById (selectObj );
} // Clear option
RemoveOptions (selectObj); // option count
Var start = 0; // If you need to add the first option
If (firstOption ){
SelectObj. options [0] = new Option (firstOption, ''); // The Option count starts from 1.
Start ++;
  }     
Var len = optionList. length;
For (var I = 0; I <len; I ++) {// Set option
SelectObj. options [start] = new option(optionlistpolici}.txt, optionList [I]. val); // the selected item
If (selected = optionList [I]. val ){
SelectObj. options [start]. selected = true;
} // Count Plus 1
Start ++;
 } 
} // -->
Var sidArr = [];


<? PHP

 

$ SQL = "select * from ptypes where sid = 0 and ssid = 0 ";
$ Result = mysql tutorial _ query ($ SQL, $ conn) or die ("database query tutorial error 1 ");
While ($ row = mysql_fetch_object ($ result ))
{
$ I = 0;
$ Rs2 = "";
$ Sql2 = "select * from ptypes where sid = '". $ row-> id ."'";
$ Result2 = mysql_query ($ sql2, $ conn) or die ("Data Query error 2 ");
While ($ row2 = mysql_fetch_object ($ result2 ))
   {
If ($ I = 0 ){
$ T = "";
} Else {
$ T = ",";
    }
$ Rs2. = $ t. "{txt: '". $ row2-> title. "', val: '". $ row2-> id. "'} n ";
$ N = 0;
$ Rs3 = "";
$ Sql3 = "select * from ptypes where ssid = '". $ row2-> id ."'";
$ Result3 = mysql_query ($ sql3, $ conn) or die ("query indicates database error 3 ");
While ($ row3 = mysql_fetch_object ($ result3 ))
    {
If ($ n = 0)
    {
$ T1 = "";
} Else {
$ T1 = ",";
    }
$ Rs3. = $ t1. "{txt: '". $ row3-> title. "', val: '". $ row3-> id. "'} n ";
$ N = 1;
    }
   
Echo ("sidArr ['". $ row2-> id. "'] = [". $ rs3. "];");
$ I = 1;
   }
  
Echo ("sidArr ['". $ row-> id. "'] = [". $ rs2. "];");
}
?>
Function setCity (province ){
       
RemoveOptions (document. getElementById ('Sid '));
RemoveOptions (document. getElementById ('ssid '));
SetSelectOption ('Sid ', "",'-Select -');
SetSelectOption ('ssid ', "",'-Select -');
If (! SidArr [province] | sidArr [province] = ""){
      
Document. getElementById ('Sid '). disabled = true;
Document. getElementById ('ssid '). disabled = true;
} Else {
Document. getElementById ('Sid '). disabled = false;
Document. getElementById ('ssid '). disabled = false;
SetSelectOption ('Sid ', sidArr [province],'-Select -');
   }
}
Function setssid (province ){
RemoveOptions (document. getElementById ('ssid '));
SetSelectOption ('ssid ', "",'-Select -');
If (! SidArr [province] | sidArr [province] = ""){
Document. getElementById ('ssid '). disabled = true;
} Else {
Document. getElementById ('ssid '). disabled = false;
SetSelectOption ('ssid ', sidArr [province],'-Select -');
   }
}
</Script>

<Select name = "province" id = "province" onchange = "if (this. value! = '') SetCity (this. options [this. selectedIndex]. value);">
<Option value = "">-Select-</option>
<? Php Tutorial
$ SQL = "select * from ptypes where sid = 0 and ssid = 0 ";
$ Result = mysql_query ($ SQL, $ conn) or die ("An error occurred while returning the data record! ");
While ($ row = mysql_fetch_object ($ result ))
   {
Echo ("<option value = '". $ row-> id. "'>". $ row-> title. "</option> ");
   }
?>
</Select> Level 1
  
<Select name = "sid" id = "sid" onchange = "if (this. value! = '') Setssid (this. options [this. selectedIndex]. value);">
<Option value = "">-Select-</option>
</Select> Level 2

<Select name = "ssid" id = "ssid">
<Option value = "">-Select-</option>
</Select> level 3

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.