Multiselect not working with POST method.
Posted: Wed Sep 09, 2009 12:43 pm
I am trying to use a multiple selection in HTML and store the values in an array. Then I try to get the values from the array but I get only one value. Below is the code of the HTML form. Only the first selection is retreived when using the POST method, it works fine when using the GET method. I would appreciate if someone will help me in this.
<html>
<head></head>
<body>
<?php
// check for submit
if (!isset($_POST['submit'])) {
// and display form
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<SELECT multiple name="artist[]" SIZE=5>
<OPTION>Option 1</option>
<OPTION>Option 2</option>
<OPTION>Option 3</option>
<OPTION>Option 4</option>
<OPTION>Option 5</option>
<OPTION>Option 6</option>
</SELECT>
<input type="submit" name="submit" value="Select">
</form>
<?php
}
else {
$name = $_POST['artist'];
// or display the selected artists
// use a foreach loop to read and display array elements
if (is_array($_POST['artist'])) {
echo 'You selected: <br />';
foreach ($name as $a) {
echo "<i>$a</i><br />";
}
}
else {
echo 'Nothing selected';
}
}
?>
</body>
</html>
<html>
<head></head>
<body>
<?php
// check for submit
if (!isset($_POST['submit'])) {
// and display form
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<SELECT multiple name="artist[]" SIZE=5>
<OPTION>Option 1</option>
<OPTION>Option 2</option>
<OPTION>Option 3</option>
<OPTION>Option 4</option>
<OPTION>Option 5</option>
<OPTION>Option 6</option>
</SELECT>
<input type="submit" name="submit" value="Select">
</form>
<?php
}
else {
$name = $_POST['artist'];
// or display the selected artists
// use a foreach loop to read and display array elements
if (is_array($_POST['artist'])) {
echo 'You selected: <br />';
foreach ($name as $a) {
echo "<i>$a</i><br />";
}
}
else {
echo 'Nothing selected';
}
}
?>
</body>
</html>