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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<option> selected fix with database input

Status
Not open for further replies.

rpg121

Programmer
Mar 14, 2001
89
US
Hi,

I have a PHP/mySQL database running, and I've realized I'm come across an awkward little bug.

I have a form where a user fills out their information including Province/State and Country using populated drop down boxes. This information gets stored in the database. Then, if a user wants to edit their information later, they can go to the Edit Details page which calls up a page almost identical to the original form when they first set themselves up. Of course, the person's information is pulled from the database, and is echoed as the values of the form elements.

My issue is that the two drop down boxes do not echo the value of the province/state or Country properly. They are updated in the database correctly, but the drop downs don't know how to select the appropriate values.

The <select> tag doesn't support a value option, and I can't hardcode the selected="selected" into any specific <option> tag because the input from the database needs to determine which one that is.

Any suggestions anyone has would be greatly appreciated. Thanks!

Josh Bart
MCP, A+, Network+
 
Simply output the option elements server side, and in that loop, determine if the current option is the selected one or not, and if so, add a selected attribute.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
This is a server-side question, and as such should be posted in the PHP forum here: forum434.

However to get you stared:

You are going to have to dynamically create the drop downs to populate the selected option. And check at the moment of creation whether an option has already been selected.

Something like:

Code:
if($valuefromDB==$id_of_current_option){
echo "<option selected=selected value=x>Option 1</option>";
}
else{
echo "<option value=x>Option 1</option>";
}

if($valuefromDB==$id_of_current_option){
echo "<option selected=selected value=y>Option 2</option>";
}
else{
echo "<option value=y>Option 2</option>";
}
...

Its not very elegant. but i think it gets the job don.









----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top