Create Form Elements with Multiple Options in PHP

Description

The following code shows how to create Form Elements with Multiple Options.

Example


//index.php//from  w  w w.j ava  2 s.c  o  m
<html>
<body> 
<?php
if ($_POST ['submitted'] == "yes") {
  if (count ( $_POST ['fruit'] ) != 0) {
    echo "Your Selections:<br />";
  } else {
    echo "You have not made any selections.<br /><br />";
  }
  for($i = 0; $i < count ( $_POST ['fruit'] ); $i ++) {
    echo $_POST ['fruit'] [$i] . "<br />";
  }
  ?><a href="index.php">Try Again</a><?php
}
if ($_POST ['submitted'] != "yes") {
?> 
<form action="index.php" method="post">
<input type="hidden" name="submitted" value="yes" /> Your Choice (s): <br />
<select name="fruit[]" multiple="multiple">
  <option value="Bananas">Bananas</option>
  <option value="Apples">Apples</option>
  <option value="Oranges">Oranges</option>
</select><br />
<input type="submit" value="Submit" /></form> 
<?php
}
?> 
</body>
</html>




















Home »
  PHP Tutorial »
    Form »




PHP Form
Form Demo