Forum Moderators: coopster

Message Too Old, No Replies

Array varible not passed thru $ POST

Multi select List box

         

am_25

4:03 am on Feb 14, 2007 (gmt 0)

10+ Year Member



Hi,
I'm using a multi select list box, the items in the list box are generated dynamically from a DB. I like the selected values to be passed to the script using a $_POST method.
But there are no values returned in the array.

I tried couple of different things and it does not work.
When I don't have list generated from the DB it works fine.
Please help.
Here is the code

<?php
include "connect.php";

echo '<pre>' . print_r($_POST,true) . '</pre>';
if (isset($_POST['submit']))
{
$testarray = array();
$testarray=$_POST['test'];

if ($testarray){
echo " you are inside the loop array not empty<br>";
}
echo "You selected ".$testarray." value. <br />";
if ($testarray){
foreach ($testarray as $t){echo 'You selected ',$t,'<br />';}
}
}
?>
<html>
<body>
<form action="multiple.php" method="POST">
<?php
$sql2="SELECT distinct(designation) from empdet order by designation asc ";
$result2=mysql_query($sql2);?>
<SELECT name="test[]" multiple ="multiple">
<?php
While ($row=mysql_fetch_assoc($result2))
{
//$desi=$row['designation'];
echo "<OPTION value=\"{$row['designation']}\">{$row['designation']}</OPTION>\n";
}?></select>
<input type='submit' name='submit' value='submit'>
</form>
</body>
</html>

eelixduppy

12:50 pm on Feb 14, 2007 (gmt 0)



Welcome to WebmasterWorld, am_25!

You have some errors in the code that echos the array values, however the print_r function should still show that the values are being passed. Change it to the following:


if(!isset($_POST['test'])) {
echo 'Not set!';
exit;
}
$testarray = $_POST['test'];
foreach($testarray as $t){
echo 'You selected '.$t.'<br />';
}

This should work as intended.

Good luck! :)