php 表单select元素多选实例

时间:2016-06-17
select元素在表单提交中使用频率非常高,本文章向大家介绍php 表单select元素多选实例,实例介绍了select表单元素如何实现多选以及在服务器端如何获取select多选列表的值,需要的朋友可以参考一下。

HTML代码:

<html>
<head>
<title>php 表单select元素多选实例</title>
</head>
<body>
<div>
<form action="index.php" method="post">
<p><input type="text" name="user" /></p>
<p><textarea name="address" rows="5" cols="40">
 </textarea></p>
<p><select name="products[]" multiple="multiple">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
</select></p>
<p><input type="submit" value="hit it!" /></p>
</form>
</div>
</body>
</html>

php服务器端代码:

<html>
<head>
<title>Reading Input from the Form</title>
</head>
<body>
<div>
 <?php
  print "Welcome <b>" . $_POST ['user'] . "</b><br/>";
  print "Your address is:<br/><b>" . $_POST ['address'] . "</b><br/>\n";
  
  if (is_array ( $_POST ['products'] )) {
    print "<p>Your product choices are:</p>";
    print "<ul>";
    foreach ( $_POST ['products'] as $value ) {
      print "<li>$value</li>\n";
    }
    print "</ul>";
  }
  ?>
 </div>
</body>
</html>

大家可以将代码部署在自己电脑上运行一下。