Page 1 of 1

php list box (multiple selection)

Posted: Sat Jan 03, 2009 11:35 pm
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...

Re: php list box (multiple selection)

Posted: Sun Jan 04, 2009 1:19 am
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 />';}
    }
?>