Multiselect not working with POST method.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
edcaru
Forum Newbie
Posts: 2
Joined: Wed Sep 09, 2009 12:30 pm

Multiselect not working with POST method.

Post 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>
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Multiselect not working with POST method.

Post 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
edcaru
Forum Newbie
Posts: 2
Joined: Wed Sep 09, 2009 12:30 pm

Re: Multiselect not working with POST method.

Post 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.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Multiselect not working with POST method.

Post 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
Post Reply