Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 7th, 2009, 03:40 PM
Friend of Wrox
 
Join Date: Mar 2004
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).
 
Old June 8th, 2009, 03:49 PM
Friend of Wrox
 
Join Date: Jun 2008
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.
 
Old June 12th, 2009, 03:24 PM
Friend of Wrox
 
Join Date: Mar 2004
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!
 
Old June 12th, 2009, 06:42 PM
Friend of Wrox
 
Join Date: Jun 2008
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.
 
Old June 18th, 2009, 11:44 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the info...





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





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