RE: [PHP] Multi Selection
| From: | esctoday.com | wouter van vliet | Date: | Tue, 03 Jun 2003 19:06:13 +0000 |
| Subject: | RE: [PHP] Multi Selection | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-150048@lists.php.net to get a copy of this message | ||
What you need to generate as HTML looks smth like this
---
<FORM method=post action=page.php>
<SELECT name='somename[]' size=10 multiple id='somename[]'>
<OPTION value='one'>First Thingie</OPTION>
<OPTION value='two'>Second Thingie</OPTION>
<OPTION value='three'>Third Thingie</OPTION>
<OPTION value='four'>Fourth Thingie</OPTION>
<OPTION value='five'>Fifth Thingie</OPTION>
</SELECT>
<INPUT TYPE='post'>
</FORM>
---
page.php should look a bit like this:
---
<?php
foreach($_POST['somename'] as $Key => $Value) {
$Query = 'INSERT INTO table (value) VALUES ('.$Value.')';
mysql_query($Query);
};
?>
---
What I'm not really sure about are the '[]' in the <SELECT
name='somename[]'> .. might wanna try to leave them out. Also, i'm not sure
if the value of $_POST['somename'] is always an array, also if one option is
selected.. If it gives an errer, just use something like:
<?php
if(is_array($_POST['somename'])) {
foreach($_POST['somename'] as $Key => $Value) {
$Query = 'INSERT INTO table (value) VALUES ('.$Value.')';
mysql_query($Query);
};
} else {
$Query = 'INSERT INTO table (value) VALUES ('.$_POST['somename'].')';
mysql_query($Query);
}
?>
Just to be sure of the correct POST data, I usually do a <?php
print_r($_POST); ?> .. just to test it..
Hope it has been of any help,
Wouter
-----Oorspronkelijk bericht-----
Van: Frank Keessen [mailto:fkeessen@planet.nl]
Verzonden: dinsdag 3 juni 2003 20:55
Aan: php-general@lists.php.net
Onderwerp: [PHP] Multi Selection
Hi Guys,
I'm trying to do the following:
I've got a multiple selection box build out of a query of table
<td><select name="VALUE[]" size="10" multiple
id="VALUE[]">
<?
$query2 = "SELECT SomethingID, Somewhere FROM Someplace WHERE publiceer =
'yes' ORDER BY somewhere";
$result2 = mysql_query($query2) or die ("Error in query: $query2. " .
mysql_error());
if (mysql_num_rows($result2) > 0)
{
while($row2 = mysql_fetch_object($result2))
{
?>
<OPTION VALUE="<? echo $row2->SomethingID ?>"><? echo
$row2->Somewhere ;
?>
<?
}
}
?>
</option>
</select>
The outcome is stored in the VALUE[]..
Now i want to have an INSERT query that insert each value seperatly..
So if there are 3 selections it must be repeat the INSERT query three
times...
Thanks for the help!
Frank