Value from a Multiple Select

vinitha
vinitha used Ask the Experts™
on
Hi,

how can get the values from a select having multiple items selected.
<select name="t" multiple>
<option value=1>name1</option>
<option value=2>name2</option>
<option value=3>name3</option>
<option value=4>name4</option>
</select>

when i try to post the form the $_post['t']  returns only the last index e.g if name2 and name3 are selected ; only name3 is  returned.

help asap.

regards,

vinitha
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Just make ur select variable an array to receive the multiple values. Like this <select name="t[]" multiple>

now when u will post the form u will get all the selected values in this array, which u can access it like this

echo $t[0]; //value selected 1
echo $t[1]; //value selected 2
echo $t[2]; //value selected 3

This works as I have checked.
------------------------------------
<select name="t[]" multiple>
<option value=1>name1</option>
<option value=2>name2</option>
<option value=3>name3</option>
<option value=4>name4</option>
</select>

--------------------------------------

Hope this will solve your problem
Commented:
Look at this example it should work

<?php
if ($HTTP_POST_VARS['action'] == 'submitted') {
    print '<pre>';

    print_r($HTTP_POST_VARS);
    print '<a href="'. $HTTP_SERVER_VARS['PHP_SELF'] .'">Please try again</a>';

    print '</pre>';
} else {
?>
<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
    Name:  <input type="text" name="personal[name]"><br>
    Email: <input type="text" name="personal[email]"><br>
    Beer: <br>
    <select multiple name="beer[]">
        <option value="warthog">Warthog</option>
        <option value="guinness">Guinness</option>
        <option value="stuttgarter">Stuttgarter Schwabenbräu</option>
    </select><br>
    <input type="hidden" name="action" value="submitted">
    <input type="submit" name="submit" value="submit me!">
</form>
<?php
}
?>

Hope it will help you
Best Regards
Ferhad

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial