Core PHP

A Form with Multiple Selections

This PHP program example demonstrates how to process a form that allows multiple selections.

MultipleSelectionForm.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net's PHP: Form - Multiple Select Example</title>
  </head>
  <body>

    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
      The Tribes of Israel:<br />
      <select name="keyTribesList[]" size="8" multiple>
      	<option value="kReuben">Reuben</option>
      	<option value="kSimeon">Simeon</option>
      	<option value="kLevi">Levi</option>
      	<option value="kJudah">Judah</option>
      	<option value="kIssachar">Issachar</option>
      	<option value="kZebulun">Zebulun</option>
      	<option value="kDan">Dan</option>
      	<option value="kNaphtali">Naphtali</option>
      	<option value="kGad">Gad</option>
      	<option value="kAsher">Asher</option>
      	<option value="kBejamin">Bejamin</option>
      	<option value="kJoseph">Joseph</option>
      </select><br />
      <input type="submit" name="keySubmit" value="Submit Tribes List"/>
    </form>

<?php
  if (array_key_exists('keySubmit', $_GET)) {
    $sTribes = join("<br />", $_GET['keyTribesList']);
    echo "<br /><b>You submitted these values:</b><br />".$sTribes;

    // Use a foreach loop to print each key with its value
    echo "<br /><br /><b>The _GET array:</b><br />";
    foreach ($_GET as $key => $value) {
      echo "{$key} => {$value}<br />";
    }

	echo "<br /><b>The _GET['keyTribesList'] array:</b><br />";
    foreach ($_GET['keyTribesList'] as $key => $value) {
      echo "{$key} => {$value}<br />";
    }
    echo "<br />";
  }
?>

  </body>
</html>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.