Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 7th, 2009, 03:40 PM
Friend of Wrox
 
Join Date: Mar 2004
Location: Ottawa, , Canada.
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default fun with <select> via DB...

Hey,

I'm populating form elements with data from a DB, but how do I select the <select tag with the the entry?

For example - I have this:
<select name="fruit">
<option value="Apple">Apple</option>
<option value="Orange">Orange</option>
<option value="Banana">Banana</option>
</select>

in the DB, the data saved is Orange... so how can I get so that when the form loads, the value of Orange is selected instead of Apple (default).
Reply With Quote
  #2 (permalink)  
Old June 8th, 2009, 03:49 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

The most basic way is the simplest:
Code:
<%
Set RS = yourConnection.Execute( "SELECT fruit FROM sometable WHERE something = " & somevalue )
fruit = RS("fruit")
%>
<select name="fruit">
    <option <%if fruit = "Apple" response.write "selected"%>>Apple</option>
    <option <%if fruit = "Orange" response.write "selected"%>>Orange</option>
    <option <%if fruit = "Banana" response.write "selected"%>>Banana</option>
</select>
....
If you only have a few <option>s this works fine.

Now, if you are *ALSO* getting the <option>s from the database, there's a much better way. Ask again if you are doing that.

Of if you have dozens and dozens of <option>s there are other ways that might be better. Again, ask again if so.
Reply With Quote
  #3 (permalink)  
Old June 12th, 2009, 03:24 PM
Friend of Wrox
 
Join Date: Mar 2004
Location: Ottawa, , Canada.
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeah, I agree that the aforementionned method woudl work, but I happen to have quite a few <option> tags... for multiple <select>.... I'm not pulling the option from the table, though that woudl make things a lot simpler... I have a form that the elements are already all written... just need to plop the information in there...

So if you have a better way of selecting the correct slected... I'm all ears!
Reply With Quote
  #4 (permalink)  
Old June 12th, 2009, 06:42 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Well, one way is to rewrite you page:
Code:
<select name="fruit">
<%
fruitopts = Array("apple","banana","orange")
priorFruit = RS("fruit") 
For f = 0 To UBound(fruitopts)
    fruit = fruitopts(f)
    If fruit = priorfruit Then  sel = " SELECTED " Else sel = ""
    Response.Write "<option " & sel & ">" & fruit & "</option>" & vbNewLine
Next
%>
</select>
The other way is to use JS code. Problem is, you have to delay the use of the JS code until the <FORM> is fully loaded, at least.

But anyway:
Code:
<script>
function matchPriorSelect( sel, value )
{
    for ( var o = 0; o < sel.options.length; ++o )
    {
        var opt = sel.options[o];
        var optval = opt.value;
        if ( optval == "" ) optval = opt.text;
        if ( optval == value ) 
        {
            opt.selected = true;
            return;
        }
    }
}
</script>

and then somehow you have to do something like this:

<body onload="matchPriorSelect(document.FormName.fruit, '<%=RS("fruit")%>' );">
Except you probably don't want to dump that into the onload, in case you are using various JS libraries that could overwrite it, so probably you just want to dump the call at the end of your page, after the </form> tag.
Reply With Quote
  #5 (permalink)  
Old June 18th, 2009, 11:44 AM
Friend of Wrox
 
Join Date: Mar 2004
Location: Ottawa, , Canada.
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the info...
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
<select> </select> prabhash_singh11 PHP How-To 2 March 11th, 2008 09:08 AM
select="<b>" AlDugan XSLT 8 March 24th, 2006 02:01 PM
<style> tags in a <body> vs. <div> bcat BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 March 27th, 2005 08:50 AM
HELP: XSL -> HTML <select selected=true> jedbartlett XSLT 4 October 7th, 2004 11:16 PM
<marquee><b>About CHAT App. in PHP4</b></marquee> Ramkrishna PHP How-To 1 September 11th, 2004 07:01 AM



All times are GMT -4. The time now is 05:50 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.