lonewolf217 Posted June 17, 2008 Share Posted June 17, 2008 I am still pretty new to programming with databases, I only jumped into asp and sql a few days ago. I have made excellent progress on my little project but I have hit one minor roadblock that I am hoping to get help with. If i have a form with dropdown menus, i.e. <select name="list"> <OPTION>1 <OPTION>2 <OPTION>3 </select> and this data gets entered into the database in some appropriate table. Later on, I want to give the user the opportunity to import this data back into the form to edit and resave the entry. I have been able to do it properly with text and textarea form fields, but I cannot figure out how to load the previously saved value into the SELECT dropdown list. They always just show up as the default value, and it would be up to the user to remember to change them back to the proper value. Any help would be appreciated. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted June 17, 2008 Share Posted June 17, 2008 http://www.w3schools.com/TAGS/tag_option.asp You assign the <option> tag to have a standalone attribute of SELECTED.. <option value="cat" SELECTED>Cat</option> How you get the database value to match the appropriate option tag is another story. I have the same problem as you do, how do I 'easily' retrieve and show the selected option from a stored value. One way would be have asp write out the option tags for you and do a check {if (current option'ss value == stored database value) .. write <option value="somevalue" SELECTED> .. else .. write <option value="somevalue">} (you may hardcode an array of possible values and have it print the array) I do not know ASP, but hopefully you understand the pseudo code. Also, if there is an easier way you find or someone else does, tell me, I'd like to know as well! Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted June 17, 2008 Author Share Posted June 17, 2008 I know of the way of checking each option individually against the result set but that would be extremely messy code so i was really hoping for an alternative. If there isn't, then I guess ill start writing my IF statements Quote Link to comment Share on other sites More sharing options...
haku Posted June 17, 2008 Share Posted June 17, 2008 I don't know .asp, only php, so I will give you the theory you want to use in plain English, and you can put that into asp code. select value from database create array and fill with values of 1, 2 and 3 output <select name="list"> to the screen loop through each value in the array from step two { output <option if array value is equal to value from database (step 1) { output selected="selected" to the screen } output > output array value } Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted June 17, 2008 Author Share Posted June 17, 2008 thanks, I was able to manipulate the code using an array and a simple strComp so it wasn't as bad as I thought for reference this is what it ended up being <form name="form" method="post" action="update_entry.asp"> <input type="hidden" name="ID" value="<%=rs("ID")%>"> <b><h3>Product:</h3></b> <select name="Product"> <% Dim prodArray,prod prodArray = Array("prod1","prod2","prod3","prod4") for each prod in prodarray Response.Write("<OPTION") if(strComp(prod,rs("Product"),1)=0) then Response.Write(" selected=""selected"">") else Response.Write(">") end if Response.Write(prod) next %> Quote Link to comment Share on other sites More sharing options...
haku Posted June 18, 2008 Share Posted June 18, 2008 There you go. That was exactly what I was talking about. (I don't know ASP, but I can grasp what is going on when I read it. Kinda like reading German or French...). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.