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

populating an input text box with a dropdown list box 1

Status
Not open for further replies.

samflex

IS-IT--Management
Jun 28, 2004
45
US
Assume for a moment that I have this code that dynamically populates a
dropdown listbox by querying the database(and this code works by the way),
how can I get this code populate an input textbox?

In other words, I want to select a value from this dropdown and populate an input textbox with value selected.

I am bit up by this code.

Can you please bell me out again?

Thanks in advance.

Below is what I have so far.

Code:
<script>

function moveText()
{
window.document.testform.testtext.value = window.document.testform.street.options[window.document.testform.street.selectedIndex].value
}

</script>
<body>
<table>
<tr>
   <td><select name="street" size="1" name="street" onChange="moveText()">>
     <option selected>&lt;Choose One&gt;</option>
     <option>------------------------------------------------</option>

<%
dim Conn,SQLstr
Function OpenDBFConn(Path)
  Dim Conn: Set Conn = CreateObject("ADODB.Connection")
  Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                   "Data Source=" & Path & ";" & _
                   "Extended Properties=""DBASE IV;"";"
  Set OpenDBFConn = Conn
End Function

Dim DBConn
Set DBConn = OpenDBFConn("\\tpd")
'Open recordset from basins table

Dim rsWaterShed
Set rsWaterShed = DBConn.Execute("SELECT * FROM table order by site_code")

          While not rsWaterShed.EOF

       %>

         <OPTION value="<%=rsWaterShed(1)%>"><%=rsWaterShed(1)%></OPTION>

       <%

         rsWaterShed.MoveNext

        wend

        rsWaterShed.close

        set rsWaterShed=nothing
     %>
    </select></td>
<td><input type="text" name="testtext" size="30"></td>
</tr>
</table>
</body>
 
I messed with this for awhile.
You'll have to substitute your database calls back in.

Code:
<script type="text/javascript">

<!-- hide from old browsers
function moveText()
{
var txt
txt=document.forms[0].street.options[document.forms[0].street.selectedIndex].text
document.forms[0].testtext.value=txt
}

//end -->
</script>

<%
	[b]strconn="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="
	strconn=strconn & server.mappath("/theozarkerr2/Data/thomasadd2000.mdb") & ";"
	set conn=server.createobject("adodb.connection")
	conn.open strconn
	set rs = Server.CreateObject("ADODB.recordset")
	strsql="select lastname from addlist group by lastname"
	rs.Open strsql, conn, 3, 3[/b]
%>
<body>
<form method="POST" action="test.asp" name="testform">
<table>
<tr>
<td><select name="street" size="1" onChange="moveText()">
<option selected>Choose One</option>
<option>------------------------------------------------</option>
<%
    do While not [b]rs[/b].EOF
		response.write "<OPTION value=>" & rs("lastname") & "</OPTION>"
       [b]rs[/b].MoveNext
    loop
[b]rs.close
set rs=nothing
set conn = nothing[/b]
%>
</select></td>

<td><input type="text" name="testtext" size="30"></td>
</tr>
</table>
</form>
 
You are great, MoLaker!

It is working real good.

Thank you so very much!!!!

I tell you, I can't think of a better more talented forum than this one.
 
Can I ask you one additional question please.

If I wanted to change this slightly to say:

When I select a name from the dropdown list box, populate an input textbox with an ID associated with that name, if that ID exists, how difficult is this?

Thanks really very much (again) for all the help.
 
If you extracted the associated ID along with the name from the database, you could pass the ID through to the Javascript function and store that passed value.

Code:
<script type="text/javascript">

<!-- hide from old browsers
function moveText([b]ID[/b])
{
document.forms[0].testtext.value=[b]ID[/b]
}

//end -->
</script>

<%
    strconn="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="
    strconn=strconn & server.mappath("/theozarkerr2/Data/thomasadd2000.mdb") & ";"
    set conn=server.createobject("adodb.connection")
    conn.open strconn
    set rs = Server.CreateObject("ADODB.recordset")

    strsql="SELECT * FROM table order by site_code"
    rs.Open strsql, conn, 3, 3
[b]do While not rs.EOF[/b]
%>
<body>
<form method="POST" action="test.asp" name="testform">
<table>
<tr>
<td><select name="street" size="1" onChange="moveText([b]<%=rs("ID")%>[/b])">
<option selected>Choose One</option>
<option>------------------------------------------------</option>
<%
        response.write "<OPTION value=>" & rs("name") & "</OPTION>"
       rs.MoveNext
    loop
rs.close
set rs=nothing
set conn = nothing
%>

Or something like that. You'd have to move the start of your do while so that ID would be available to insert into the form's onChange call.

I didn't test it, so it might require tweaking, but should be workable.
 
Thanks again Molaker,
I am playing with it now to resolve some issues.

It seems to be populating a textbox with a record from dropdown but the dropdown box is not dropping down.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top