HTML代码:

<html>
<head>
< >php 表单select元素多选实例</ >
</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>
< >Reading Input from the Form</ >
</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>

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

收藏 打印