1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title></title> 5 <meta charset="utf-8"> 6 </head> 7 <body> 8 <form action="" method="get"> 9 <select multiple="multiple" name="city[]"> 10 <option value="">請選擇城市</option> 11 <option value="北京">北京</option> 12 <option value="上海">上海</option> 13 <option value="廣州">廣州</option> 14 <option value="深圳">深圳</option> 15 <option value="大連">大連</option> 16 </select> 17 <input type="submit" value="點擊"> 18 </form> 19 <?php 20 header("Content-Type:text/html;charset=utf-8"); 21 22 $selected=isset($_GET["city"])?$_GET["city"]:""; 23 if(is_array($selected)){ 24 foreach($selected as $key =>$value){ 25 echo "city is".$value."<br>"; 26 } 27 } 28 ?>
解釋:這裏的<select multiple="multiple" name="city[]"> ,屬性被設置爲multiple,意爲可多選,
name的值被修改爲了數組。
將city爲名傳遞給selected變量,因爲selected變量如今爲數組,所以須要使用foreach遍歷數組將value的結果所有打印出來。
29
30 </body>
31 </html>
顯示結果:php
改變選擇模式,使用圓形符號做爲選框:html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title></title>
5 <meta charset="utf-8">
6 </head>
7 <body>
8 <form action="#" method="post">
9 <input type="radio" name="test1" value="test1">
10 test1 11 <input type="radio" name="test1" value="test2">
12 test2 13 <input type="radio" name="test1" value="test3">
14 test3 15 <input type="submit" name="提交">
16 </form>
17
18 <?php 19 $a=isset($_POST['test1'])?$_POST['test1']:''; 20 if($a){ 21 echo $a; 22 }else{ 23 echo "you not choose"; 24 } 25 ?>
26
27 </body>
28 </html>
顯示結果:數組
當選擇時:post
對於表單多選時:spa
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title></title> 5 <meta charset="utf-8"> 6 </head> 7 <body> 8 <form action="#" method="post"> 9 <input type="checkbox" name="ch[]" value="test1"> 10 test1 11 <input type="checkbox" name="ch[]" value="test2"> 12 test2 13 <input type="checkbox" name="ch[]" value="test3"> 14 test3 15 <input type="checkbox" name="ch[]" value="test4"> 16 test4 17 <input type="submit" value="提交"> 18 </form>
// 注意:多選框的type爲:checkbox,name須要標記爲數組的類型,必定要寫value,而且value的內容不相同
19 <?php 20 $a=isset($_POST['ch'])?$_POST[ch]:''; 21 if(is_array($a)){ 22 foreach ($a as $key => $value) { 23 echo $value."<br>"; 24 } 25 }else{ 26 echo "you not choose"; 27 } 28 ?>
29 </body>
30 </html>
顯示結果:3d