JavaScript combined with PHP to implement the dynamic implementation of double Pull-down menu in Web page _jsp programming

Source: Internet
Author: User

This article introduces the code of the dynamic implementation of the double Pull-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, assuming that the main menu has been constructed with HTML, because the submenu needs to be dynamically designed, 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> 
 
<select name= "Smenu" >//Sub-menu design 
 
</select> 

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, which uses the phplib 
 
$mmenu = Array ("A", "B", "C");//Here I make a simplified 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 is used 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 Menu Union array fill 
 
unset ($smenu);//delete smenu variable 
 
} 
 
?>//End PHP Program 
With (document) { 
 
id=all ("Mmenu"). Value;//Get the value of the main menu 
 
Arr_menu=menu[id]; 
 
For (I=all ("Smenu"). options.length;i>=0;i--) {All 
 
("Smenu"). Options.remove (i);//need to clear the original item 
 
} 
 
if ( arr_menu.length==0) {return 
 
; 
 
} 
 
for (i=0;i<arr_menu.length;i++) { 
 
obj=createelement ("option"); 
 
Obj.text=arr_class[i]; 
 
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; 
 

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:

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