javascript+php Application One: The dynamic realization 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>
<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, 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. "");
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.