...

View Full Version : problem with to select multiple items



swethak
07-25-2008, 01:56 PM
hi

i wrote a code to select multiple items in a drop down list.And i store all the items in my database.But in that i select multiple items and submit that items last item only stored.Please tell that what's the mistake in my code.




<td><select name="imagelist" id="imagelist" multiple="multiple">
<?
while($row=mysql_fetch_array($result))
{
$i=1;
$urlname=$row['urlname'];
?>

<option value="<?=$urlname?>"><?=$ext=strrchr($urlname,'/');?></option>

<?
}
?>
</select>


And i store these items in a database with a name $_POST['imagelist'].

Anybody plz tell that whats the mistake in my code.plz help me.

djm0219
07-25-2008, 02:07 PM
Change
<td><select name="imagelist" id="imagelist" multiple="multiple"> to
<td><select name="imagelist[]" id="imagelist" multiple="multiple"> and you should get the options selected back to PHP in an array named imagelist. You will of course have to get the POST values into your variable using:

$imagelist = $_POST['imagelist'];
print_r($imagelist); // show the values that were selected

swethak
07-25-2008, 02:53 PM
Change
<td><select name="imagelist" id="imagelist" multiple="multiple"> to
<td><select name="imagelist[]" id="imagelist" multiple="multiple"> and you should get the options selected back to PHP in an array named imagelist. You will of course have to get the POST values into your variable using:

$imagelist = $_POST['imagelist'];
print_r($imagelist); // show the values that were selected


And also i want to store all the items in $imagelist in my database.How i store like that?plz tell that

djm0219
07-25-2008, 03:28 PM
And also i want to store all the items in $imagelist in my database.How i store like that?plz tell that

Depends on the table being used and what columns you have defined.

JohnDubya
07-25-2008, 03:33 PM
And also i want to store all the items in $imagelist in my database.How i store like that?plz tell that

Well, $imagelist = $_POST['imagelist']; will put all the items into an array in the $imagelist variable. Then, you can do a foreach() on $imagelist and insert them in your database or whatever you need to do:


foreach ($imagelist as $key => $value) {
$url = mysql_real_escape_string($value);
$insert = mysql_query("INSERT INTO images SET url = '$url'");
}

This is pretty basic PHP. I would encourage you to read up on the basics of PHP.

http://www.w3schools.com/PHP/DEfaULT.asP
http://www.tizag.com/phpT/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum