javascript+php Application One: The dynamic implementation of double Pull-down menu in Web page making (paste)

Source: Internet
Author: User
Tags array implement mysql return
Menu | news | web | dropdown javascript+php Application One: Dynamic implementation of double Pull-down menu in Web page making

---excerpted from the Internet

In the Web page production, often encountered this situation, through the main Drop-down menu selection, dynamic generation of sub Drop-down menu. For example: In the main menu has "Focus News", "Life Style", "Mood Story" three options, through the "Focus News" choice, submenu automatically generated such as "domestic", "international", "Sports", "entertainment", and so on.

Using JavaScript, we can easily achieve these results. The problem is that if the options in the menu are dynamically extracted from the database (or other files), it's not easy to implement. The author according to own practical experience, introduces a kind of use JavaScript + PHP realization method, the text database uses MySQL. In this case, I will also describe how to return the selection status of the last menu option after each form submission.

The purpose of PHP described in this article is to extract menu options from the database, and to generate JavaScript code in another role. Readers can use an interpreted language of their own familiarity, such as ASP.

To simplify the code, the author assumes that the main menu has been constructed by HTML and that the submenu needs to be dynamically designed so that only the basic frame is drawn and the HTML code is as follows:

<select name= "Mmenu" onchange= "java Script:setmenu ()" >//Main Menu design

<option value= "A" > Focus news </option>

<option value= "B" > Life fashion </option>

<option value= "C" > Mood story </option>

Value must be consistent with the menu array below


<select name= "Smenu" >/Sub Menu design


What we need to consider is what steps the onchange () event of the menu needs to complete. The approximate process is to construct a submenu item based on the options of the main menu. The item text of the submenu is best set beforehand. According to this idea, the author adopts the Joint Array Record submenu option in JavaScript, which is automatically generated by PHP when loading. Therefore, the author designed the following JavaScript function SetMenu ():

function SetMenu () {

Menu=array ("A", "B", "C"); Construct Menu Union array

<?php//Start PHP Program

$db = new my_db ();

$db->database = "* * *"; Constructs a new MySQL connection, where the phplib is used

$mmenu = Array ("A", "B", "C"); Here the author makes a simplification

for ($i =0; $i <count ($mmenu); $i + +) {

$id = $mmenu [$i];

$db->query ("Select Menu from class where MenuID = '". $id. "");

Suppose the menu option is stored in the menu field in the Class table, MenuID to identify the menu

while ($db->next_record ()) {

$smenu [] = "". $db->f ("menu"). "";


if (Isset ($smenu) && Is_array ($smenu)) {

$str = Implode (",", $smenu);

echo "menu[" $id "] =array ($STR); NTT";

Complete the fill of the menu Union array

Unset ($smenu); Delete Smenu variable



?>//End PHP Program

With (document) {

Id=all ("Mmenu"). Value; Get the value of the main menu


For (I=all ("Smenu"). options.length;i>=0;i--) {

All ("Smenu"). Options.remove (i); Need to clear the original project


if (arr_menu.length==0) {



for (i=0;i<arr_menu.length;i++) {

Obj=createelement ("option");


All ("Smenu"). Options.add (obj);




So each time the document is displayed, the PHP section is interpreted as a JavaScript language, and the submenu is automatically updated when the main menu is clicked. By the same token, readers can create more complex multiple menu options based on this idea.

Finally, the author briefly describes how to implement the status of the last time the menu item is persisted after the form is submitted. There are many techniques, but the author uses the implicit variable method. Add the following code in the form:

<input type= "hidden" name= "H1" >

<input type= "hidden" name= "H2" >

We only need to assign a value to each implied variable in the onsubmit () event of the form form. That

document.all ("H1"). Value=document.all ("Mmenu"). SelectedIndex;

document.all ("H2"). Value=document.all ("Smenu"). SelectedIndex;

To take advantage of the implied variables, in the onload () event of the body of the document, we use the PHP method (and other methods) to control the display of the menu:


if (!isset ($h 1)) {//Only need to judge $h1

$h 1 = 0;

$h 2 = 0;


echo "document.all (" Mmenu "). selectedindex=". $h 1. "; NTT ";

echo "document.all (" Mmenu "). Click (); NTT";

echo "document.all (" Mmenu "). selectedindex=". $h 1. "; NTT ";

echo "document.all (" Smenu "). selectedindex=". $h 2;


So far, we have implemented the dynamic implementation method of the double Pull-down menu.

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: 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.