Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help! -- dropdowns and populating text boxes

    Hi everyone! I don't know much about javascript, but I think I need to use it for what I want to do for my project.

    I have a form in which I have a drop down box with ID numbers in it. When I select an ID number, I want to be able to populate the form with variables associated with the ID number. For example, when I select ID number 1, I want to write John in textbox1, John's last name, Doe, in textbox2, John Doe's address in textbox3.

    Is there a way to split up the option values in order to place them into seperate textboxes? Right now, I have one long string that writes into one textbox.

    This is what I have so far:

    <%
    strSQL = "SELECT * FROM tblStudent"
    Set rs = conn.execute(strSQL)

    Do While Not rs.EOF
    %>

    <option value="<%=rs("StudentID")%>&nbsp;<%=rs("FirstName")%>&nbsp;<%=rs("LastName")%>"><%= rs("StudentID")%></option>
    <%
    rs.MoveNext
    Loop
    %>


    and then I have this so that it updates the textbox when an ID is selected:

    <td><input type="text" name="FirstName" size="30" tabindex="2"></td>
    <script language="javascript">
    function MoveID()
    {
    var FirstName = document.me.IDNumber.value;
    document.me.FirstName.value = FirstName;
    }
    </script>

    Is there a way to take the LastName from the option value and write it to the LastName textbox?

    Thanks in advance!

  • #2
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i think i found the solution to my original question. now i'm just wondering how to loop through the case function now? what is the syntax?

    someone please help.

    <script language="javascript">
    function fillFields(Select)
    {
    var Form1 = Select.form;
    switch (Select.options[Select.selectedIndex].value)
    {
    case '<%= rs("StudentID")%>':Form1.FirstName.value='<%= rs("FirstName")%>';Form1.LastName.value='<%= rs("LastName")%>';break;
    }
    }
    </script>

    <select size="1" name="IDNumber" tabindex="1" onchange="fillFields(this)">
    <option value=" ">&nbsp;</option>
    <%
    Do While Not rs.EOF
    %>
    <option value="<%= rs("StudentID")%>"><%= rs("StudentID")%></option>
    <%
    rs.MoveNext
    Loop
    %>
    </select>
    Last edited by xo_tiff_xo; 12-17-2004 at 08:24 PM. Reason: HELP!

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,045
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Try this:
    Code:
    <%
    dim strOptions, strID
    %>
    <script language="javascript">
    function fillFields(Select)
    {
      var Form1 = Select.form;
      switch (Select.options[Select.selectedIndex].value)
      {
        <%
        while not rs.EOF and not rs.BOF
           strID = rs("StudentID")
           strOptions = strOptions & "<option value=""" & strID & """>" & strID & "</option>" & vbCrLf
        %>
        case "<%= strID%>":Form1.FirstName.value="<%= rs("FirstName")%>";Form1.LastName.value="<%= rs("LastName")%>";break;
        <%
          rs.Movenext
        wend
        %> 
        default: //for the first empty option, reset fields(?)
          Form1.FirstName.value="";
          Form1.LastName.value="";
          break;
      }
    }
    </script>
    
    <select size="1" name="IDNumber" tabindex="1" onchange="fillFields(this)">
    <option value="">&nbsp;</option>
    <%=strOptions%>
    </select>

  • #4
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks so much, Glenn! The code is exactly what I wanted it to do. You're a genius !! Take care.

  • #5
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    same problem

    I have a very similar problem, if not the same problem, as this however I cannot get the code to work. Here is what I have copy pasted / edited so far....

    <?PHP
    $username="ommitted";
    $password="ommitted";
    $database="GasPrices";

    mysql_connect(localhost,$username,$password);

    @mysql_select_db($database) or die( "Unable to select database");
    $query= "SELECT * FROM gastable ORDER BY Name ASC";
    $result=mysql_query($query)or die( mysql_error());
    ?>
    </?PHP



    <%
    dim strOptions, Name
    %>
    <HTML>
    <HEAD>
    <script language="javascript">
    function fillFields(Select)
    {
    var Form1 = Select.form;
    switch (Select.options[Select.selectedIndex].value)
    {
    <%
    while not rs.EOF and not rs.BOF
    Name = rs("StudentID")
    strOptions = strOptions & "<option value=""" & Name & """>" & Name & "</option>" & vbCrLf
    %>
    case "<%= Name%>":Form1.FirstName.value="<%= rs("FirstName")%>";Form1.LastName.value="<%= rs("LastName")%>";break;
    <%
    rs.Movenext
    wend
    %>
    default: //for the first empty option, reset fields(?)
    Form1.FirstName.value="";
    Form1.LastName.value="";
    break;
    }
    }
    </script>
    </HEAD>
    <BODY>
    <select size="1" name="stationname" tabindex="1" onchange="fillFields(this)">
    <option value="">&nbsp;</option>
    <%=strOptions%>
    </select>
    </BODY>
    </HTML>


    Basically I am trying to generate a list of the gas stations in my table in the drop down box. The list will be a concatenated listing of station name / address. For example:

    name = Chevron
    address = 1 Main Street

    concatenated version = "Chevron - 1 Main Street"

    That is how they will appear in the drop down box. Once the user selects one of those it will split it into the appropriate name / address boxes. For example:

    they select: "Chevron - 1 Main Street"
    box1 value - "Chevron"
    box2 value - "1 Main Street"

    Someone please help I am ripping my hair out!!!


  •  

    Posting Permissions

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