Filling TextBox Using Change in ListBox(Database Related)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vini171285
    New Member
    • Apr 2008
    • 60

    Filling TextBox Using Change in ListBox(Database Related)

    Hi,
    Actually i am filling a list box from database and depending on change in that list box ,the value corresponding to that selected index should come in the textbox,which should come from another column of the table in database..
    What code should be written for this??
    Thanx..
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    #2
    Hi,

    You can do it this way:
    Code:
      <html>
      <head>
       <script language = "javascript">
         function showText()
          {
            //This functions is used show selected value of listbox into textbox.
             var objselect;
             var txtval;
            objselect=document.forms['testform'].testselect;
    
           //Get the selected value from listbox
            txtval=objselect.options[objselect.selectedIndex].value;
    
           //Display it in textbox 
            document.getElementById('Text1').value = txtval; 
          }
    
       </script>
      </head>
       <body>
       <form>
        <%
          Dim con
          Dim rs
          Dim sql
    
          set con= server.CreateObject("ADODB.Connection")
          set rs= server.CreateObject("ADODB.Recordset")
     
            '''Open connection
          con.open "...."  
          sql = "Select column1,column2 from table1"
    
          ''Open recordset
          set rs= con.execute(sql)
    
         %>  
       <Select id="testSelect" onSelectedIndexChanged ="showText();">
      <%While not rs.Eof%>
       <Option value="<%=rs(0)>"><%=rs(1)></Option>
      <%rs.movenext
       Wend>
      </Select>
      <input type="text" id= "Text1" value=""></input>
       </form>
       </body>
        </html>

    Originally posted by Vini171285
    Hi,
    Actually i am filling a list box from database and depending on change in that list box ,the value corresponding to that selected index should come in the textbox,which should come from another column of the table in database..
    What code should be written for this??
    Thanx..

    Comment

    • Vini171285
      New Member
      • Apr 2008
      • 60

      #3
      Hi
      This code is not working...
      Actually i am having a list box and after u change the value of list box ,the price corresponding to the selected index should come in textbox..
      This is my code...

      Code:
      <html>
      <body>
      Select a item:
      <select name=fruit>
      <%
      	Dim objConn
      	Set objConn=Server.CreateObject("ADODB.Connection")
      	objConn.ConnectionString="DSN=Info.dsn;uid=vini;pwd=vini"
      	objConn.Open
      
      	Dim sql
      	sql="SELECT * FROM fruits"
      
      	Dim objRS
      	Set objRS=Server.CreateObject("ADODB.Recordset")
      	objRS.Open sql,objConn
      
      	Do while not objRS.EOF
      		Response.Write "<option value'" & objRS("fnum") & "'>"
      		Response.Write objRS("fruit") & "</option>"
      		objRS.MoveNext
      	Loop
      %>
      </select>
      <%
      	objRS.Close
      	Set objRS=Nothing
      
      	objConn.Close
      	Set objConn=Nothing
      %>
      <BR><input type=text name=price>
      </body>
      </html>
      and our database table is like this..

      Table
      fnum fruit price
      app Apple 300
      mng Mango 1000
      grp Grapes 500

      Comment

      • DrBunchman
        Recognized Expert Contributor
        • Jan 2008
        • 979

        #4
        You can store the price in the value property of your Drop Down options. Then you can use Shweta's bit of javascript to populate the textbox.

        Like so:
        Code:
        Do while not objRS.EOF 
        		Response.Write "<option value'" & objRS("price") & "'>"
        		Response.Write objRS("fruit") & "</option>"
        		objRS.MoveNext
        Loop
        Will this solution work for you? It may be that you need to store fnum in the value field of your Drop Down List in which case we'll need to be a bit cleverer.

        Let me know how this works for you.

        Dr B

        Comment

        • Vini171285
          New Member
          • Apr 2008
          • 60

          #5
          Hi!
          The code we tried,its working, but it is not the solution to our problem..
          If we store price as value in option,then while storing it in database we cant store the price because it can be same for many options..
          and it will create problems while retrieving data..
          Is there any other solution??

          Comment

          • DrBunchman
            Recognized Expert Contributor
            • Jan 2008
            • 979

            #6
            In that case you need some way to store both the fnum AND the price when you make your initial query of the database. There are a few ways you can do this but probably the simplest is to store both of them in the value field of the Drop Down but separate them with a special character. Take a look at this example:
            Code:
             	
            Do while not objRS.EOF 
               Response.Write "<option value'" & objRS("fnum") & "*" & objRS("price") &"'>"
               Response.Write objRS("fruit") & "</option>"
               objRS.MoveNext
            Loop
            You can see that both fnum and price are stored as the value and are separated by the * character. Now you need to retrieve each of the values both in your javascript function when you populate the textbox and also in your asp code when you want to use the fnum value. Here is an example of each:

            Javascript:
            Code:
             var sOption = document.FormName.fruit.options[document.FormName.fruit.selectedIndex].value;
             var sOptionArray = sOption.split("*");
             document.FormName.price.value = sOptionArray[1];
            VBScript:
            Code:
            If Instr(Request("fruit"), "*") > 0 Then
            	  fnum = Split(Request("fruit"), "*")(0)
            	  price= Split(Request("fruit"), "*")(1)
            End If
            Let me know how you get on.

            Dr B

            Comment

            • Vini171285
              New Member
              • Apr 2008
              • 60

              #7
              HI..

              Code:
              <html>
              <head>
                 <script language = "javascript">
                   function showText()
                    {
                     if ((document.TestForm.fruit.options[document.TestForm.fruit.selectedIndex].value)==300)
              	    {
              	    document.TestForm.Text1.value=document.TestForm.fruit.options[document.TestForm.fruit.selectedIndex].text;
              	    }
              	    else
              	    {
              	    document.TestForm.Text2.value=document.TestForm.fruit.options[document.TestForm.fruit.selectedIndex].text;
              	    }
                    }
                    function show()
                    {
                    var sOption = document.TestForm.fruit.options[document.TestForm.fruit.selectedIndex].value;
              	  var sOptionArray = sOption.split("*");
              	  document.TestForm.price.value = sOptionArray[1];
              	   }
              
              
                 </script>
                </head>
              <body>
              <form method=post name=TestForm action="d5.asp">
              Select a item:
              <select name=fruit onchange="show();">
              	<option value='1*300'>Apple</option>
              	<option value='2*300'>Mango</option>
              	<option value='3*500'>Grapes</option>
              </select><BR>
              
              <input type=text name=Text1 id=Text1><BR>
              <input type=text name=Text2 id=Text2>
              </body>
              </html>
              I went through the code that you provided but,I am not getting the correct position to place the vbscript code..
              Just tell me where exactly to place the vb-script part in this code

              Thanx !

              Comment

              • DrBunchman
                Recognized Expert Contributor
                • Jan 2008
                • 979

                #8
                You don't need to the vbscript on this page but you will need it on the page that this form loads when it submits in order to get the values that are passed through the request object.

                The code you've printed above should work just fine.

                Has it worked okay for you?

                Dr B

                Comment

                • Vini171285
                  New Member
                  • Apr 2008
                  • 60

                  #9
                  Hi Dr B,

                  Thanx for the solution ...

                  The code is working well...
                  Thanx!!

                  Comment

                  • DrBunchman
                    Recognized Expert Contributor
                    • Jan 2008
                    • 979

                    #10
                    No problem Vini, glad it's working for you.

                    Comment

                    Working...