Jump to content

Multiple Lists Selection / PHP


liquidd

Recommended Posts

Hey all,

 

I am still learning PHP, which is the best language for database driven sites. I am using Dreamweaver CS3 with the devleopers toolbox which is "ok" to help take the guess work out of things.

 

I am using a simple registration form that i made with the toolbox, but when I come to a multiple list to choose from (populated from the database) it doesn't work correctly. Of course, out of the multiple items selected only the last one gets inserted.

 

From what I have read, the selection must be turned into an array then looped for this to work. Is this correct? Next how would I code this to work right.

 

If you let me know which code to post, I will post it.

 

Thanks for all your help!!

Link to comment
https://forums.phpfreaks.com/topic/61578-multiple-lists-selection-php/
Share on other sites

Yes, you must name the select tag as an array with square brackets. Take this example:

 

<?php
if(isset($_POST['submit'])){
    foreach($_POST['select'] as $value){
        echo 'You selected '.$value.'<br />';
    }
}
?>
<form action="<?php echo $_SERVER['phpself']; ?>" method="post">
<select name="select[]" multiple="multiple">
<option value="var1">Var 1</option>
<option value="var2">Var 2</option>
<option value="var3">Var 3</option>
<option value="var4">Var 4</option>
<option value="var5">Var 5</option>
</select>
<br />
<input type="submit" name="submit" />

If you were to select all 5 options, the output would be:

You selected var1
You selected var2
You selected var3
You selected var4
You selected var5

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.