Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop down list is writing only one word to database

Status
Not open for further replies.

kellie2

Technical User
Feb 6, 2006
7
US
I have a drop down list which contains about twenty options. Each options contains more than one word. When a user selects an option the selected item is supposed to be displayed in the database. This works ok but the problem is that only the first word of the option selected is displayed in the database. For example if the option 'Foil Drift' is selected from the list only 'Foil' is displayed in the database. Here is the code I am using to insert the data into the database

<?PHP
echo "<select name = errorgroup>";
while(list($Error)=mysql_fetch_row($result)){
echo "<option value =$Error>$Error</option>\n";
}
echo "</select>";
$errorgroup=$_POST['errorgroup'];

$sql = "INSERT INTO redo( ErrorGroup) values( '$errorgroup')";

$result=mysql_query($sql,$db);

?>

Does anyone know how I can get this code to display the full option selected in the database?

 
In the HTML that produces the form, are you outputting:

<option value="Foil Drift">Foil Drift</option>

or

<option value=Foil Drift>Foil Drift</option>


If you do the second one, only the first word of the value will actually be used. Your browser will interpret the space outside of quotes as a divider between attributes.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Sorry should have mentioned that bit. The drop down list is being populated by reading the values from a database. The code I am using for populating the drop down list is as follows:

<?PHP
$sql = "Select Error, Counter From errorgroup";
$result=mysql_query($sql,$db);
$rowResult=mysql_fetch_assoc($result);
$totalRows=mysql_num_rows($result);

echo "<select name = errorgroup>";
while(list($Error)=mysql_fetch_row($result)){
echo "<option value =$Error>$Error</option>\n";
}
echo "</select>";
?>
 
Again, the problem is missing quotes around your value in your <option> tag.

If you have an <OPTION> tag (and this also is true for other tags) and an attribute's value has a space and you don't put quotes around that value:

<OPTION value=foo bar>BAZ</option>

Then your browser will treat the space as the end of the value and the next word as another attribute, unrecognized and ignored. To set a multiword value for an attribute, you must surround the value with quotes:

<OPTION value=[red]"[/red]foo bar[red]"[/red]>BAZ</option>

Actually, it's a good idea to get into the habit of putting quotes around all attribute values all the time.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Ok thats great thanks for the quick reply. I will give this a try now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top