Jump to content

[SOLVED] Question about retrieving database information back into forms


lonewolf217

Recommended Posts

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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
%>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.