Php mysql js drop-down box Level 2 Linkage

Source: Internet
Author: User

The select drop-down list (select) is composed of values from the database. The second-level list is a multi-choice list (multiple). To implement the second-level list, you can change the multiple menu.

<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> full-Dynamic secondary linkage drop-down list </title>
</Head>
<Body>
<?
/*************************************** ********
** Functions: PHP + mysql implements a level-2 cascade drop-down box
** Database: Database Name (db_city), data table (t_province, t_city)
** Fields in the t_province table: id (id number) and provinceName (province name)
** Fields in the t_city table: id (id number), provinceId (province ID), and cityName (city name)
**************************************** *******/

// ******************* Select a database connection ***************
$ Link = mysql_connect ("localhost", "root", "123 ")
Or die ("cocould not connect:". mysql_error ());
Mysql_select_db ("db_city") or die ("cocould not select database ");

// ******************* Extract province information ***************** *
$ SqlSel = "select * from t_province order by id ";
$ Result = mysql_query ($ sqlSel) or die ("Query failed:". mysql_error ());

$ Forum_data = array ();
While ($ row = mysql_fetch_array ($ result ))
{
$ Forum_data [] = $ row;
}
// Print_r ($ forum_data );
Mysql_free_result ($ result );

// ************** Obtain city information **************
$ SqlSel2 = "select * from t_city order by provinceId desc ";

If (! ($ Result2 = mysql_query ($ sqlSel2 )))
{
Die ('could not query t_city list ');
}

$ Forum_data2 = array ();
While ($ row2 = mysql_fetch_array ($ result2 ))
{
$ Forum_data2 [] = $ row2;
}

Mysql_free_result ($ result2 );
?>

<! -- ************ JavaScript processing province -- onChange *************** -->
<Script language = "JavaScript">
Var onecount2;
Subcat2 = new Array ();
<? Php
$ Num2 = count ($ forum_data2 );
?>
Onecount2 = <? Echo $ num2;?>;
<?
For ($ j = 0; $ j <$ num2; $ j ++)
{
?>
Subcat2 [<? Echo $ j;?>] = New Array ("<? Echo
$ Forum_data2 [$ j] ['id'];?> "," <? Echo
$ Forum_data2 [$ j] ['provinceid'];?> "," <? Echo
$ Forum_data2 [$ j] ['cityname'];?> ");
<?}?>
Function changelocation (id)
{
Document. myform. city. length = 0;
Var id = id;
Var j;
Document. myform. city. options [0] = new Option ('= select city = ','');
For (j = 0; j <onecount2; j ++)
{
If (subcat2 [j] [1] = id)
{
Document. myform. city. options [document. myform. city. length] = new Option (subcat2 [j] [2], subcat2 [j] [0]);
}
}
}
</Script>

<! -- ******************** Page form ***************** * ******* -->
<Form name = "myform" method = "post">
Address: <select name = "bigClass"
OnChange = "changelocation (document. myform. bigClass. options [document. myform. bigClass. selectedIndex]. value )"
Size = "1">
<Option selected> select a province </option>

<? Php
$ Num = count ($ forum_data );

For ($ I = 0; $ I <$ num; $ I ++)
{
?>
<Option value = "<? Echo $ forum_data [$ I] ['id'];?> "> <? Echo $ forum_data [$ I] ['provincename'];?> </Option>
<?
}
?>
</Select>
<Select name = "city" multiple>
<SELECT name = city size = 1 id = "city">
<Option selected value = "" >== select city ==</option>
</Select>
</Form>
</Body>
</Html>

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.