Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2004
    Posts
    22

    Unanswered: populate text field from value selected from drop down menu

    I have a drop down menu which is populated from a database. The database contains, amongst other info, staff name, staff no.

    The drop down displays the staff names.

    On selecting the staff name required I need to store the appropriate staff no in a text box.

    I know I need to use the Onchange function but I don't know how to code the function.

    I have searched everywhere and not found any code which works.
    Any help would be gratefully rec'd

  2. #2
    Join Date
    Dec 2003
    Posts
    39
    u may use client side script

    <script language="Javascript">
    function showNo()
    {
    frmStaff.staffNo.value=frmStaff.staffName.value;
    }
    </script>
    <%
    dim cn, rs
    set cn=server.createobject("ADODB.CONNECTION")
    set rs=server.createobject("ADODB.RECORDSET")
    cn.Open "Provider=SQLOLEDB.1;Data Source=servername;Initial Catalog=dbname;User ID=uid;Password=pwd;"
    rs.Open "select staffNo, staffName from staff", cn, 3, 1
    %>
    <form name="frmStaff" method="get">
    <select name="staffName" onChange="showNo();">
    <%
    while not rs.eof
    response.write "<option value=" & rs("staffNo") & ">" & rs("staffName") & "</option>"
    rs.movenext
    wend
    %>
    </select>
    <input type="text" name="staffNo" value="" enabled="false">
    </form>
    <%
    rs.close
    cn.close
    set rs=nothing
    set cn=nothing
    %>
    Link Link

  3. #3
    Join Date
    Nov 2005
    Location
    San Francisco, CA
    Posts
    506
    Hi,
    try this.....
    <script language="JavaScript" type="text/JavaScript">
    function joy()
    {
    window.document.form2.textfield.value=window.docum ent.form1.select.value
    }
    </script>

    write the database population_combo code here...
    <select name="select" SIZE=10 SINGLE onChange="joy()">

    Joydeep

  4. #4
    Join Date
    Nov 2005
    Location
    Honolulu HI
    Posts
    119
    geez guys.. a little overkill....

    lindentree,
    when you are populating the <OPTION>staff nanme</option>
    instead, include the Staff ID Number

    <OPTION value='<%=intStaffIDNumber%>'>staff nanme</option>

    it would end up looking like this


    Code:
    <html>
    <body>
    
    <form name="frmData">
    
    <select name="lstEmpList" SINGLE onChange="frmData.txtTextID.value=frmData.lstEmpList.value;">
    <OPTION value='1'>Staff 1</option>
    <OPTION value='2'>Staff 2</option>
    <OPTION value='3'>Staff 3</option>
    <OPTION value='4'>Staff 4</option>
    <OPTION value='5'>Staff 5</option>
    </select>
    
    <input type="text" name="txtTextID">
    
    </form>
    </body>
    </html>
    .
    .
    http://www.HawaiianHope.org
    Providing Technology services to non profit orgs, Homeless Shelters, Food Pantries, Clean And Sober Houses and more.
    To date we have given away over 900 free computers !
    __________________________
    caeli enarrant gloriam Dei !

  5. #5
    Join Date
    Nov 2004
    Posts
    22
    thanks for all the help - I really like the simplicity of the Kropes2001 solution - the only problem I still have is that I have to store both the staff name and the staff number. I am now working through the other solutions.

  6. #6
    Join Date
    Nov 2005
    Location
    Honolulu HI
    Posts
    119
    "I have to store both the staff name and the staff number"

    can you clarify that a little...
    i thought it was already in the database ?
    .
    .
    http://www.HawaiianHope.org
    Providing Technology services to non profit orgs, Homeless Shelters, Food Pantries, Clean And Sober Houses and more.
    To date we have given away over 900 free computers !
    __________________________
    caeli enarrant gloriam Dei !

  7. #7
    Join Date
    Nov 2004
    Posts
    22
    I have a staff names database which holds (amongst other things)
    Staff First name
    Staff Surname
    Staff no.

    I am using this data to populate a new database of 'staffactions'

    I have now used your code in my dropdown as follows:

    <select name="selName" class="maintext" id="selName" SINGLE onChange="form1.staffno.value=form1.selName.value; " >
    <%
    While (NOT Staffnames.EOF)
    %>
    <option value="<%=(Staffnames.Fields.Item("FirstName").Val ue)&" "&(Staffnames.Fields.Item("Surname").Value)%>"><%= (Staffnames.Fields.Item("FirstName").Value)&" "&(Staffnames.Fields.Item("Surname").Value)%> </option>
    <%
    Staffnames.MoveNext()
    Wend
    If (Staffnames.CursorType > 0) Then
    Staffnames.MoveFirst
    Else
    Staffnames.Requery

    End If
    %>

    I then have the staffno field which has an initial allocation from the staffnames database - to cover the possibility that the first person on the list is selected:

    <input name="staffno" type="text" id="staffno" value="<%=(Staffnames.Fields.Item("StaffNumber").V alue)%>" size="6" />

    So what I now need to do is to get the Single Onchange function to put the correct corresponding StaffNumber into the textbox

    Many thanks for all your help

  8. #8
    Join Date
    Nov 2005
    Location
    Honolulu HI
    Posts
    119
    <option value="<%=(Staffnames.Fields.Item("FirstName").Val ue)&"

    change that to
    <option value="<%=(Staffnames.Fields.Item("StaffNumber").V alue)&"

    put the number there instad of their name.
    Last edited by kropes2001; 02-03-06 at 07:02.
    .
    .
    http://www.HawaiianHope.org
    Providing Technology services to non profit orgs, Homeless Shelters, Food Pantries, Clean And Sober Houses and more.
    To date we have given away over 900 free computers !
    __________________________
    caeli enarrant gloriam Dei !

  9. #9
    Join Date
    Nov 2004
    Posts
    22
    but I need to store all 3 values.

    By amalgamating the first name and surname in the drop down (both values and labels) I am storing their names into one field but I have to store the staff number as well as I have more than one John Smith. the new database is then forming a historical record so I can't refer back to the staffnames database for additional information as this only holds current staff.

    Thanks for all your help by the way. I really is appreciated!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •