Re: PHP and listbox multiple selections
| From: | sundogcurt | Date: | Thu, 31 Jan 2002 20:39:23 +0000 |
| Subject: | Re: PHP and listbox multiple selections | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-82991@lists.php.net to get a copy of this message | ||
<form name="" action="" method="">
Notice the "[ ]" at the end of the select box name, this prompts php to treat the values as an array.
<select name="options[]" size="10" multiple>
<?
$query = "select value1,value2 from table";
if($result=mysql_query($query)){
while ($myrow = mysql_fetch_row($result)) {
print "<option value='$myrow[0]'>$myrow[1]";} } ?> </select> <input type="submit" value="submit"> </form> When the form is submitted I fire this code $mycount = count($options); // get the number of elements in the array (selected in the drop down) while($mycount){// cycle through the array decrementling your counter var ($mycount) untill you have inserted all of the values.
$mycount--;
mysql_query("INSERT INTO table (value1,value2) VALUES ($value1,$options[$mycount])");
}
I hope this helps, it works perfectly for me and uses less code than other examples I have seen.
On Monday, January 28, 2002, at 10:41 AM, Simos Varelakis wrote:A php form contains a listbox (named foolist) with n Items which can be multiple selected and form posted to results.php how can results.php determine which items were selected in order to add all the items in a Mysql table ???