Get value from select control with foreach loop in PHP

Description

The following code shows how to get value from select control with foreach loop.

Example


<html>// w w  w.j ava  2s.c  o m
<body>
<form action="formSelectData.php" method="POST">
  <input type="text" name="user">
  <br>
  <textarea name="address" rows="5" cols="40"></textarea>
  <br>
  <select name="products[]" multiple>
    <option>option1
    <option>option2
    <option>option3
    <option>option4
  </select>
  <br>
  <input type="submit" value="OK">
</form>
</body>
</html>

The following code is for formSelectData.php.


<html>// w w w. j av a2  s . c  o m
<head>
<title>Reading input from the form</title>
</head>
<body>
<?php
print "Welcome <b>$user</b><p>\n\n";
print "Your address is:<p>\n\n<b>$address</b><p>\n\n";
print "Your product choices are:<p>\n\n";
print "<ul>\n\n";
foreach ( $products as $value ){
    print "<li>$value<br>\n";
}
print "</ul>";
?>
</body>
</html>




















Home »
  PHP Tutorial »
    Form »




PHP Form
Form Demo