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

Populate text field from dropdown list 1

Status
Not open for further replies.

optjco

IS-IT--Management
Apr 13, 2004
185
GB
I wonder if someone can help me I have a table in a DB with two fields (Supplier & SupplierID. In my ASP page I am populating a dropdown list with names from the Supplier field, what I would like to do is automatically populate a text field with the corresponding SupplierID value, for example

Supplier SupplierID
Paul 1
Tom 2
Harry 3

Therfore if i select "Tom" in the dropdown then the value 2 will be populated in the textbox

Below is teh code I have at the moment

Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%> 
<!--#include file="../Connections/GosportNC.asp" -->
<%
Dim RSsupp
Dim RSsupp_numRows

Set RSsupp = Server.CreateObject("ADODB.Recordset")
RSsupp.ActiveConnection = MM_GosportNC_STRING
RSsupp.Source = "SELECT *  FROM Supplier  WHERE SupplierType = 'B' AND Supplier <> 'All'  ORDER BY Supplier ASC"
RSsupp.CursorType = 0
RSsupp.CursorLocation = 2
RSsupp.LockType = 1
RSsupp.Open()

RSsupp_numRows = 0
%>
<html>
<head>
<title>NewNCboard</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<%
Dim strUKDate, MYdd, MYmm, MYyy
MYdd = DatePart("d", date())
MYmm = DatePart("m", date())
MYyy = DatePart("yyyy", date())
strUKDate = (MYdd & "/ " & MYmm & "/ " & MYyy)
%>
</head>

<body>
<form name="form1" method="post" action="">
  <table width="85%" border="0" align="center" cellpadding="1" cellspacing="1" style = "border-bottom: 1px solid grey; border-left: 1px solid grey; border-top: 1px solid grey; border-right: 1px solid grey">
    <tr> 
      <td height="27" colspan="4"> <div align="center"><font size="3" face="Verdana, Arial, Helvetica, sans-serif"><strong><u>Gosport 
          Board Supplier Non Conformance</u></strong></font></div></td>
    </tr>
    <tr> 
      <td width="20%"><div align="right"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Supplier 
          :</font></div></td>
      <td width="39%"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp; 
        <select name="cmbSupplier" id="select">
          <option value=" "></option>
          <%
While (NOT RSsupp.EOF)
%>
          <option value="<%=(RSsupp.Fields.Item("Supplier").Value)%>"><%=(RSsupp.Fields.Item("Supplier").Value)%></option>
          <%
  RSsupp.MoveNext()
Wend
If (RSsupp.CursorType > 0) Then
  RSsupp.MoveFirst
Else
  RSsupp.Requery
End If
%>
        </select>
        <input name="txtID" type="text" id="txtID" size="5" maxlength="5">
        </font></td>
      <td width="21%"><div align="right"></div></td>
      <td width="20%"><div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> 
          </font></div></td>
    </tr>
  </table>
</form>
</body>
</html>
<%
RSsupp.Close()
Set RSsupp = Nothing
%>

Regards

Olly
 
Ok when you done the supplier...and they selected it...

do javascript....command to reload the page.
on the <select>

Now the page has reloaded to itself...then you can capture
the request("value") and then do another read of the table

select id from supplier where = request("value") this becomes the next box..


 
You can do it with some client-side script.
Change your select like this:
Code:
<select name="cmbSupplier" id="select" [b]OnChange="MoveID()"[/b]>
Add the client-side function:
Code:
<script language="javascript">
function MoveID()
{
var myVar = document.form1.cmbSupplier.value
document.form1.txtID.value = myVar;
}
</script>
 
Veep,
Your code works however in my dynamic dropdown I have the following

Paul
Tom
Harry

and when I select one of the names in the dropdown say for example Tom then the textbox is also populated with Tom whereas what i need is to have the supplier ID in the textbox. For example in my table the supplier name is Tom and the supplier ID id 2 therefore when i select Tom from the dropdown I want to populate the textbox with the value 2

Regards

Olly
 
That's because you are populating the Option Value attribute with the Supplier:
Code:
option value="<%=(RSsupp.Fields.Item("Supplier").Value)%>">
You'll need to change that to:
Code:
option value="<%=(RSsupp.Fields.Item("Supplier[b]ID[/b]").Value)%>">
 
Veep,
did as you suggested and now the dropdown is populated with the SupplierID's such as

1
2
3
4

then when I select one of them the textbox is also populated with the same i.e

select 1 from dropdown
textbox populated with 1

Regards

Olly
 
Code:
<option value="<%=(RSsupp.Fields.Item("[b]SupplierID[/b]").Value)%>">
<%=(RSsupp.Fields.Item("[b]Supplier[/b]").Value)%></option>
Just as a little demo copy this and save it as a .htm page.
Code:
<form name="form1">
<select name="cmbSupplier" id="select" OnChange="MoveID()">
<option value="">&nbsp;</option>
<option value="1">Tom</option>
<option value="2">Paul</option>
<option value="3>>Harry</option>
</select>
<br>
<input type="text" name="txtID">
</form>
<script language="javascript">
function MoveID()
{
var myVar = document.form1.cmbSupplier.value;
document.form1.txtID.value = myVar;
}
</script>
 
Veep,
your an absolute "star",so one is on it's way to you. thanks for persevering with me

Regards

Olly
 
i have a very similar problem,
at the moment the value tbl_warranty.warranty is displayed in the combo and the value tbl_warranty.warrid is submitted as x_warrid

i need the value tbl_warranty.cost to be displayed in a text box

Code:
<%
x_warridList = "<select name='x_warrid'><option value=''>Please Select</OPTION>"
sqlwrk = "SELECT `warrid`, `warranty`, `cost` FROM `tbl_warranty`"
Set rswrk = Server.CreateObject("ADODB.Recordset")
rswrk.Open sqlwrk, conn, 1, 2 
If Not rswrk.Eof Then
    datawrk = rswrk.GetRows
    rowswrk = UBound(datawrk, 2)
	acctotdrop = UBound(datawrk, 2)
    For rowcntwrk = 0 To rowswrk
        x_warridList = x_warridList & "<option value='" & datawrk(0, rowcntwrk) & "'"
        If CStr(datawrk(0, rowcntwrk)&"") = CStr(x_warrid&"") Then
            x_warridList = x_warridList & " selected"
        End If
        x_warridList = x_warridList & ">" & datawrk(1, rowcntwrk) & " £" & datawrk(2, rowcntwrk) & "</option>"
    Next
End If
rswrk.Close
Set rswrk = Nothing
x_warridList = x_warridList & "</select>"
Response.Write x_warridList
%>

 
have put veebs code into this
and now the texbox updates with tbl_warranty.warrid

Code:
<form name="form1">
<%
x_warridList = "<select name='x_warrid' id='select' onchange = 'moveidwarr()'><option value=''> Please Select</OPTION>"

sqlwrk = "SELECT `warrid`, `warranty`, `cost` FROM `tbl_warranty`"
Set rswrk = Server.CreateObject("ADODB.Recordset")
rswrk.Open sqlwrk, conn, 1, 2

If Not rswrk.Eof Then
    datawrk = rswrk.GetRows
    rowswrk = UBound(datawrk, 2)
    For rowcntwrk = 0 To rowswrk
    x_warridList = x_warridList & "<option value='" & datawrk(0, rowcntwrk) & "'"

        If CStr(datawrk(0, rowcntwrk)&"") = CStr(x_warrid&"") Then
            x_warridList = x_warridList & " selected"
        End If
        x_warridList = x_warridList & ">" & datawrk(1, rowcntwrk) & " £" & datawrk(2, rowcntwrk) &  "</option>"
    Next
End If
rswrk.Close
Set rswrk = Nothing
x_warridList = x_warridList & "</select>"
Response.Write x_warridList
%>
<input type="text" name="txtwarrID">

</form>

<script language="javascript">
function moveidwarr()
{
var myVar = document.form1.x_warrid.value;
document.form1.txtwarrID.value = myVar;
}
</script>
 
Burnside, I see no reference to "cost" in your code ??

Regards

Olly
 
olly,

so far tbl_warranty.cost is part of the recordset and is displayed in the dropdown list (notice the £ in the code below)
"datawrk(2, rowcntwrk)"
Code:
x_warridList = x_warridList & ">" & datawrk(1, rowcntwrk) & " £" & datawrk(2, rowcntwrk) &  "</option>"

i dont know how to get this value displayed in the textbox 'txtwarrid'
 
Hi there,

I have a similar problem. How would you populate more than one textfield with the drop box?

For example, I want to have a drop down with IDnumbers. In my database I have a first name, last name, etc associated with the IDnumber.

So on my page I have txtfield FirstName, txtfield LastName. How do I populate FirstName with the recordset first name and LastName with the recordset lastname?

Thanks in advance for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top