JavaScript + PHP application 1: webpage

Source: Internet
Author: User

JavaScript + PHP application 1: dynamic implementation of double drop-down menus in web page creation
--- From Internet
This is often the case when creating a web page. You can use the main drop-down menu to dynamically generate a sub-drop-down menu. For example, there are three options in the main menu: "Focus News", "Life Style", and "mood Story". Through the "Focus News" option, sub-menus are automatically generated, such as "domestic", "international", "Sports", and "Entertainment.
With javascript, we can easily achieve the above results. But the problem is that if the options in the menu are dynamically extracted from the database (or other files), it is not easy to implement them. Based on my actual experience, I would like to introduce you to the implementation method using javascript + php. The database in this article uses mysql. In this example, the author also describes how to return the selection status of the last menu option after each form is submitted.
The role of php introduced in this article is to extract menu options from the database and generate javascript code. Readers can use familiar explanatory languages, such as asp.
To simplify the code, I assume that the main menu has been constructed through html. As the sub-menu needs to be dynamically designed, only the basic framework is drawn. 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"> lifestyle </option>
<Option value = "c"> mood story </option>
// The value must be consistent with the menu array below
</Select>
<Select name = "smenu"> // sub-Menu Design
</Select>
We need to consider the steps required for the onchange () event of the menu. The general process is to construct a sub-menu item based on the options of the main menu. The project Text of the sub-menu should be set in advance. Based on this idea, I used the combined array record sub-menu option in javascript, which was automatically generated when php loaded. As a result, the author designs the following javascript function setmenu ():
Function setmenu (){
Menu = array ("a", "B", "c"); // constructs a menu Union array
<? Php // start the php Program
$ Db = new my_db ();
$ Db-> database = "***"; // create a new mysql connection. phplib is used here.
$ Mmenu = array ("a", "B", "c"); // simplified here
For ($ I = 0; $ I <count ($ mmenu); $ I ++ ){
$ Id = $ mmenu [$ I];
$ Db-> query ("select menu from class where menuid = '". $ id ."'");

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.