Newbie Dropdown
Moderator: General Moderators
Newbie Dropdown
Hi
How do I get multiple selected option values from a dropdown list, in a form, to insert into a table? I only seem to be able to get the first selection sent to the database. Or if I make it an array...... i.e.:
<select size="3" name="sport_type[]" multiple>
<option>baseball </option>
<option>football</option>
<option>basketball</option>
<option>soccer </option>
.......I get the just the word array in the table column.
How does it work?
Thanks
How do I get multiple selected option values from a dropdown list, in a form, to insert into a table? I only seem to be able to get the first selection sent to the database. Or if I make it an array...... i.e.:
<select size="3" name="sport_type[]" multiple>
<option>baseball </option>
<option>football</option>
<option>basketball</option>
<option>soccer </option>
.......I get the just the word array in the table column.
How does it work?
Thanks
-
Matt Phelps
- Forum Commoner
- Posts: 82
- Joined: Fri Jun 14, 2002 2:05 pm
On a similar subject (and I hope you don't mind me hijacking the thread a sec) I am looking for a way to set the value of a select box by getting it from the database.
I want someone to be able to submit their form and then later re-edit the information they sent which means that the select dropdown menu should show what it was they originally selected. Make sense?
I want someone to be able to submit their form and then later re-edit the information they sent which means that the select dropdown menu should show what it was they originally selected. Make sense?
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Or in XHTML:
Mac
Code: Select all
<select name="anything">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4" selected="selected">Four</option>
<option value="5">Five</option>
</select>-
Matt Phelps
- Forum Commoner
- Posts: 82
- Joined: Fri Jun 14, 2002 2:05 pm
Right but the problem is that the item that I want to be selected is defined by a variable in the database. I have a form that my users fill in and submit including a dropdown box. Later on they want to edit the information and so I pull the info back out of the database and into the form text fields. I need the dropdown box to 'remember' what it had selected.Peter wrote:Matt... a quick HTML lesson...
<SELECT NAME='anything'>
<OPTION VALUE='1'>One
<OPTION VALUE='2'>Two
<OPTION VALUE='3'>Three
<OPTION VALUE='4' SELECTED>Four
<OPTION VALUE='5'>Five
</SELECT>
From that, the page will load with Four selected.
If you see what I mean....
Yes, I understand but I thought with the example I gave you that you could write something like this...
Say the drop down box contained the users' favourite type of beverage...
<?
// you've connected to the database
$result = mysql_query("select drink from table where user='mr_squiggle'");
list($drink) = mysql_fetch_row($result);
// you've got an array of the type of drink
$beverages = array('coke','coffee','tea','energy drink');
echo "<select name='drink'>";
foreach($beverages as $typeofdrink){
if($typeofdrink == $drink){
echo "<option value='$typeofdrink' selected>$typeofdrink";
}else{
echo "<option value='$typeofdrink'>$typeofdrink";
}
echo "</select>";
?>
..and walla!
Say the drop down box contained the users' favourite type of beverage...
<?
// you've connected to the database
$result = mysql_query("select drink from table where user='mr_squiggle'");
list($drink) = mysql_fetch_row($result);
// you've got an array of the type of drink
$beverages = array('coke','coffee','tea','energy drink');
echo "<select name='drink'>";
foreach($beverages as $typeofdrink){
if($typeofdrink == $drink){
echo "<option value='$typeofdrink' selected>$typeofdrink";
}else{
echo "<option value='$typeofdrink'>$typeofdrink";
}
echo "</select>";
?>
..and walla!
i'm having the same issue except i'm using a abbreviation to represent the value in the database.
for example, i have a state dropdown box which contains options like:
<OPTION value="NY">New York</OPTION>
when i go back to edit the record, i want "New York" to appear in the dropdown because i had selected it earlier when i first submitted the record.
any thoughts?
for example, i have a state dropdown box which contains options like:
<OPTION value="NY">New York</OPTION>
when i go back to edit the record, i want "New York" to appear in the dropdown because i had selected it earlier when i first submitted the record.
any thoughts?
-
Matt Phelps
- Forum Commoner
- Posts: 82
- Joined: Fri Jun 14, 2002 2:05 pm
Right, here is how I finally solved the puzzle with help from Peter.
What you want is some code like this:
What happens is that for each if statement it checks to see if the variable you got out of your database (or wherever) matches that option. If it does then it echo's SELECTED in the HTML code and your dropdown menu will automatically pre-select that option.
Hope that helps!
What you want is some code like this:
Code: Select all
<option value="NY" <?php if ($myvariable== 'NY') {echo "SELECTED";} ?>>NY</option>
<option value="MI" <?php if ($myvariable== 'MI') {echo "SELECTED";} ?>>MI</option>
<option value="CA" <?php if ($myvariable== 'CA') {echo "SELECTED";} ?>>CA</option>
etcHope that helps!
thanks.
any thoughts how i would be able to map the database abbreviation to the real value.
so, if i displayed a table containing many different state abbreviations, i could map those abbrerviations to the actually state names and display.
i.e.
DB value
-----------
NY
CA
AL
Displayed value
-------------------
New York
California
Alabama
if i were to list out all of the possible values, this could make the sourcefile too big.
does anyone know of a method of creating a map file or reference file for these type of scenarios?
any thoughts how i would be able to map the database abbreviation to the real value.
so, if i displayed a table containing many different state abbreviations, i could map those abbrerviations to the actually state names and display.
i.e.
DB value
-----------
NY
CA
AL
Displayed value
-------------------
New York
California
Alabama
if i were to list out all of the possible values, this could make the sourcefile too big.
does anyone know of a method of creating a map file or reference file for these type of scenarios?
- RandomEngy
- Forum Contributor
- Posts: 173
- Joined: Wed Jun 26, 2002 3:24 pm
- Contact:
Try creating a PHP file that looks like this:
Then when you need the full state name, just include that file, and use
Another way would be creating another table in the database with all of the abbreviations and full names, but that would be less convenient than the associative array approach.
Code: Select all
<?php
$state_array = array(
"NY" => "New York",
// rest of the states
"CA" => "California"
);
?>Code: Select all
echo "State: ".$state_arrayї$dbvalue];That should work.
I also have a DB abbreviation that's already in an array when i display it.
i.e.
echo "<td>" . $row["state"] . "</td>\n";
would i be able to map that value with the associative array.
i.e.
echo "<td>" . $state_array[$row["state"]]. "</td>\n";
or do i have to assign the value in the array to another variable?
I also have a DB abbreviation that's already in an array when i display it.
i.e.
echo "<td>" . $row["state"] . "</td>\n";
would i be able to map that value with the associative array.
i.e.
echo "<td>" . $state_array[$row["state"]]. "</td>\n";
or do i have to assign the value in the array to another variable?
- RandomEngy
- Forum Contributor
- Posts: 173
- Joined: Wed Jun 26, 2002 3:24 pm
- Contact: