Page 1 of 1

Multiselect not working with POST method.

Posted: Wed Sep 09, 2009 12:43 pm
by edcaru
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>

Re: Multiselect not working with POST method.

Posted: Wed Sep 09, 2009 1:02 pm
by Mirge
edcaru wrote: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>
Not sure what the problem actually is... I copy/pasted the above verbatim... and this is what I see:

You selected:
Option 1
Option 2
Option 3

Re: Multiselect not working with POST method.

Posted: Thu Sep 10, 2009 10:36 am
by edcaru
I selected 2 options ,option 2 and option 4 but only option 2 is displayed as selected.
Here is the source of the html file that my form creates after submitting.

<html>
<head></head>
<body>

You selected: <br /><i>Option 2</i><br />
</body>
</html>

I am using tomcat5.5 and php5, though I don't think that the problem lies in any of these.

Re: Multiselect not working with POST method.

Posted: Thu Sep 10, 2009 11:37 am
by Mirge
Not really sure if tomcat would have any effect, I doubt it, but I'm not using it... so I can't be sure. It works flawlessly for me using PHP 4 and PHP 5 with Apache 1.3 and Apache 2