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

Assign value to a dropdown list

Status
Not open for further replies.

longmatch

Programmer
Nov 1, 2001
406
0
0
A form was designed for data collection. I want users to be able to modify their data afterwards. In my collection form, there are several dropdown lists powered by a database using recordset. I am just wondering how to put the value users chose back to their form when they edit? and allow them to make another choice.

big thanks

Longmatch
 
I use javascript to take the value selected and place it in a separate textbox;

function changetext5() {
selected = document.Events.Type.selectedIndex;
value = document.Events.Type.options[selected].value;
text = document.Events.Type.options[selected].text;
document.Events.text5.value = document.Events.Type.options[selected].text;
}

Then in my <Select> list box I use <SELECT size=1 NAME=&quot;Type&quot; onchange=&quot;changetext5();&quot;>.

Works for me, hope this helps. mtndew
 
I do not totally understand your answer. Could you give me more explanations. Do you mean I need to save 'selected', 'value', 'text' information back to my database for data's 'put back'? I have 12 dropdown lists with the same contents in my form. I need to put the information back to the form form my database.

Thanks

Longmatch
 
Are you looking to check and see if there is already data for that person in the database and if there is then fill it in for them? If you are the way I usually do it is to Define the Function that opens, reads, and closes the database. Select field1,field2,field3 from database where username =&quot;username of person&quot; . Then I store them as

If Rs(0) = &quot;&quot; then
field1 = &quot;Please Enter Data Here&quot;
Else Rs(0) = field1
End If
If Rs(1) = &quot;&quot; then
field2 = &quot;Please Enter Data Here&quot;
Else Rs(1) = field2
End If
If Rs(2) = &quot;&quot; then
field3 = &quot;Please Enter Data Here&quot;
Else Rs(2)= field3
End If


Then down in the form
<input type=&quot;text&quot; value=&quot;<% field1 %>&quot; >
<input type=&quot;text&quot; value=&quot;<% field2 %>&quot; >
<input type=&quot;text&quot; value=&quot;<% field3 %>&quot; >

This will fill in the current information in if it's there or if not it will ask them to enter it. I'm not sure if it's what you need, I applogize if it isn't.
 
As I understood it your user makes a selection from a <Select> list which you want to then go to a form so they they can edit and/or make another choice. If what you really want to do is have the user make their choices, save it to the database and then open a form showing what they did...thats different. I would use a sparate asp page using the addnew method and then automatically redirect to the form page showing the info they just entered. mtndew
 
Mtndew:
Just as you said, user saved their data into database and then I want to allow them to modify the data they stored in the similiar looking form. My real question is how to send their choice back to a dropdown list, which user use to make their choice and saved to database.

For example:
if my dropdown list have following items
RED
GREEN
BLUE
Purple
Yellow
Black.

and the user's data in database for this field is 'Green', how can I make it show in the dropdown list when retrieving the data from the database?

I am sorry about the misunderstanding

Longmatch
 
<% If Rs(0)=&quot;red&quot; then
soption =&quot;red&quot;
Else If Rs(0)=&quot;green&quot; then
soption=&quot;green&quot;
Else If Rs(0)=&quot;blue&quot; then
soption =&quot;blue&quot;
Else If Rs(0)=&quot;&quot; then
soption=&quot;red&quot;
End If

If soption =&quot;red&quot; then
option2 =&quot;blue&quot;
option3 =&quot;green&quot;
Else if soption=&quot;green&quot; then
option2 =&quot;red&quot;
option3 =&quot;blue&quot;
Else If soption =&quot;blue&quot; then
option2 =&quot;red&quot;
option3 =&quot;green&quot;
End If


<select size=&quot;1&quot; name=&quot;D1&quot;>
<option selected><% soption %></option>
<option><% option2 %></option>
<option><% option3 %></option>
</select><input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;B1&quot;><input type=&quot;reset&quot; value=&quot;Reset&quot; name=&quot;B2&quot;></p>
</form>

Is this what you are looking for? If the persons record shows option 1 in the database, then it will automatically have that color in there. I set it to default to red but you could change that. It uses the same basic code as before but this time with a dropdown list. It checks to see the color and based upon the colors fills in the other 2 but leaves the correct one. Is this closer to what you needed? ( I'm not sure if there is an easier way then this )
 
You need to read back the selection from the database and place it in a variable. Assuming you can do this we now have a variable say:

ColorVAR = &quot;GREEN&quot;

Now assuming you are not creating your drop down list from a database table, all you need to do is dynamically place the word &quot;selected&quot; in the correct option:

<select name=&quot;select&quot;>
<option value=&quot;RED&quot;>RED</option>
<option value=&quot;GREEN&quot; selected>GREEN</option>
<option value=&quot;BLUE&quot;>BLUE</option>
...
</select>

To do this dynamically we use our variable...

<select name=&quot;select&quot;>
<option value=&quot;RED&quot; <% If ColorVAR='RED' then response.write(&quot;selected&quot;)%>>RED</option>
<option value=&quot;GREEN&quot; <% If ColorVAR='GREEN' then response.write(&quot;selected&quot;)%>>GREEN</option>
<option value=&quot;BLUE&quot; <% If ColorVAR='BLUE' then response.write(&quot;selected&quot;)%>>BLUE</option>
...
</select>

Do you need help getting the variable set up from the database?

Hope this helps, BDC.
 
BDC:
That is what I do need. Thanks

Longmatch
 
BDC:

I have another question for you. If the dropdown list is generated dynamically, how to put the choice back? see my code below for the dropdown list.

<td><select name=&quot;cboRotation6&quot; id=&quot;cboRotation1&quot;>
<option></option>

<% rsRotation.movefirst
do until rsRotation.eof %>
<option value =&quot;<%=rsRotation(&quot;ABBR&quot;)%>&quot;><% =rsRotation(&quot;Rotation&quot;)%></option>
<% rsRotation.movenext
loop
%>
</select></td>

thanks again

longmatch

 
Do you mean you are happy with this solution or do you mean you would like more assistance? BDC.
 
I would like more assistance. Because my dropdown lists are generated dynamically.

thanks.

longmatch
 
I've just aswered this in another post thread333-521826 :)

You could put...

<option value =&quot;<%=rsRotation(&quot;ABBR&quot;)%>&quot;<% If ColorVAR=rsRotation(&quot;ABBR&quot;) then response.write(&quot; selected&quot;)%>><% =rsRotation(&quot;Rotation&quot;)%></option>


This assumes you have written the Abbreviation to the database so &quot;ColorVAR&quot; will be something like &quot;GR&quot;. BDC.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top