php list box (multiple selection)

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
chxxangie
Forum Newbie
Posts: 12
Joined: Wed Sep 19, 2007 10:00 pm

php list box (multiple selection)

Post by chxxangie »

Code: Select all

 
<select name="mentee_list" size="10" multiple="multiple" id="mentee_list">
      <?php   
        $conn = db_connect();     
        $query = "SELECT * FROM mentormentee where length(username_mentor) = 0 ";
        $result = $conn->query($query);
                    
        while($row = mysqli_fetch_assoc($result)){ ?>
              <option value="<?php echo $row["mentee"]; ?>"><?php echo $row["mentee"]; ?></option>
      <?php 
        } ?>
</select> 
 
how to get the multi value selected by user?

Code: Select all

 
$mentee_list = $_POST["mentee_list"];
 
i try this, but it just show the first value only.
i need someone to help me on this plz....thanks in advance...
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: php list box (multiple selection)

Post by watson516 »

You just have to add [] after the select name to turn it into an array and then loop through it.

Code: Select all

<form action="action.php" method="post">
<select name="test[]" multiple="multiple">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
    <option value="four">four</option>
    <option value="five">five</option>
</select>
<input type="submit" value="Send" />
</form>

Code: Select all

<?php
    $test=$_POST['test'];
    if ($test){
     foreach ($test as $t){echo 'You selected ',$t,'<br />';}
    }
?>
Post Reply