php get the value form forms list box

There are drop-down list box list boxes and menus list box in two forms, their basic syntax are the same. During the website design program, the drop-down list boxes and menus list box applications are very broad. Can be achieved by selection of the conditions of the drop-down list boxes and menus list box.

 

1. Get the value of the drop-down list box

Gets the value of the drop-down list box method is very simple, similar to get the value of a text box, you first need to define the name property value drop-down list box, and then use $ _POST [] to get the value of global variables. Marble repair factory mechanical member

Here, an example to explain the drop-down list box to get the value of this example is to select a user-specified conditions in the drop-down list box, click on the "submit" button, the value of the output condition selected by the user. Specific steps shown below:

(1) Create a index.php page, create a form form, add a drop-down list box and a submit button, the main code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<!DOCTYPE html>

<html lang="en">

<head>

   <meta charset="UTF-8">

   <title>form</title>

</head>

<body>

<form action="" method="post" name="form1">

   <table width="300"  border="0" cellpadding="0"  cellspacing="0">

      <tr>

         <td width="100" height="30" align="center" >

            <span class="style2">选择部门:</span>

         </td>

         <td width="200">

            <select name="select" size="1">

               <option value="销售部" selected>销售部</option>

               <option value="财务部" >财务部</option>

               <option value="行政部" >行政部</option>

               <option value="后勤部" >后勤部</option>

            </select>&nbsp;

           <input type="submit" name="submit" value="提交">

         </td>

      </tr>

   </table>

</form>

</body>

</html>

Note: In the present example the code, provided the size attribute, the size attribute in the <select> tag value is 1, the drop-down list box; if the value is greater than 1, it indicates that the list box, it is determined to display the size of the specified value the number of elements in the list. If the number of elements in the list greater than the value of the size attribute set, the vertical scroll bar is added automatically.

(2) write PHP statements, by $ _POST [] global variable to get the value of the drop-down list box, use echo statements output. PHP code which is shown below:

1

2

3

4

5

<?php

 if($_POST["select"]!= ""){                       //响应表单事件,返回多选列表框的值

   echo "您选择的部门是:".$_POST["select"];

 }

?>

(3) enter the run in the browser address, and press Enter to obtain the results shown below of the operation:

 

2. Get the value menu list box

When <select> tag set multiple properties, compared with the list box menu, you can select a plurality of conditions. Since the menu list box are generally a plurality of values ​​exist, for convenience of traditional values, <select> tag is usually named an array, the code format is as follows:

1

<input type="checkbox" name="checkbox[]" multiple>

You can then return the page count () function to calculate the size of the array, binding loop output for the selected menu item.

Setting a menu list box for users to choose favorite books, click the "submit" button, select the output condition value. Specific steps are as follows:

(1)新创建一个 index.php 动态页,创建一个 form 表单,添加一个菜单列表框<select>,命名为“select[]” 的数组变量,添加一个提交按钮。其主要的代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<!DOCTYPE html>

<html lang="en">

<head>

   <meta charset="UTF-8">

   <title>form</title>

</head>

<body>

<form action="index.php" method="post" name="form1">

   <table width="300"  border="0" cellpadding="0"  cellspacing="0">

      <tr>

         <td height="30" align="center" valign="middle">

 请选择喜欢的图书

 </td>

      </tr>

      <tr>

         <td align="center" valign="middle">

            <select name="select[]" size="4" multiple>

               <option value="PHP开发宝典">PHP开发宝典</option>

               <option value="数据库使用手册" >数据库使用手册</option>

               <option value="PHP从入门到精通" >PHP从入门到精通</option>

               <option value="函数大全" >函数大全</option>

            <select>

         </td>

      </tr>

      <tr>

         <td height="30" align="center" valign="middle">

            <input type="submit" name="submit" value="提交">

         </td>

      </tr>

   </table>

</form>

</body>

</html>

注意: 本实例的代码在<select>标记中设置 multiple 属性,因此,size 属性的值应与<option>标记的总数是一一对应的关系。

(2)编写PHP语句,通过$_POST[]全局变量来获取菜单列表框的值,使用echo语句输出。其PHP代码如下显示:

1

2

3

4

5

6

7

8

<?php

 if($_POST['submit']=="提交"){        //使用if条件语句判断是否提交了表单

   echo "选择的编程书籍为:";

   for($i=0; $i<count($_POST['select']);$i++){

     echo $_POST['select'][$i]."&nbsp;";    //for循环输出字符串和菜单列表框的值

   }

 }

?>

(3)在浏览器中输入运行地址,按回车键,

Guess you like

Origin www.cnblogs.com/furuihua/p/12134070.html