How to populate the dropdown values from another dropdown.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chinni1
    New Member
    • Nov 2008
    • 24

    How to populate the dropdown values from another dropdown.

    Hii..

    I Have 2 dropdowns.The first dropdown contains main products which is from database table1.
    If we select The one of dropdown value then the 2 second drop down should fill with all sub products of that main products from another table2.

    Ex:: pulsor is the main product which is in 1 st dropdown
    pulsor 150cc,pulsor 180cc ,pulsor 180cc are the sub prod s of pulsor.
    when ever we select the pulsor on 1st dropdown it should disaply all sub models. same as per all models.

    These two dropdowns should fill with from database only.

    Thanks
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    While its been awhile since ive done classic ASP (someone please correct me if I am wrong) I don't think you can capture a OnChange event in a drop down easily.

    You might have to have to submit your form to the same page and add a querystring like &submittedMain= True ...then in your page check if submittedMain=T rue and if it is then you can populate your second drop down.

    Assuming you know how to populate a record set with sql you would need sql that looks something like this

    Select ProductKey,Prod uctName from Products where MainProductKey = " & SelectedMainPro ductKey & "

    Then once you have that data in a record set use this:
    Code:
    <form>
    <select size="1" name="D1">
    <option VALUE="0" SELECTED>Select Sub Product</option><br>
    
    <%Do while not RS.eof
    Response.Write "<option VALUE=" & ProductKey & ">" & ProductName & "</option><br>"
    
    rs.movenext
    loop
    ' Exit the loop when reaching the end of the recordset
    'If rs.EOF Then Exit For end if
    'next
    'end if%>
    </select>
    </form>

    Comment

    • chinni1
      New Member
      • Nov 2008
      • 24

      #3
      Hiii...
      Thanks ur reply.I got the code.Here it is....

      strquery="Selec t * from products"
      <select name="selprod" ID="dd1" onchange="reloa d(this.form);">
      <option value="">Select prod</option>
      <%if objrs.State <> 0 then objrs.Close
      objrs.Open strquery,conn,3 ,3
      While Not objrs.eof
      prod_id=objrs(0 )
      prodname=objrs( 1)
      Response.Write "<option value='" & prod_id & "' " & strSel & ">" &prodname& "</option>"
      objrs.moveNext
      Wend
      </select>

      strquery1="Sele ct * from sub_master where id="&id
      %>
      <select name="selprod1" ID="dd2" >
      <option value="">Select sub</option>

      <%if objrs1.State <> 0 then objrs1.Close
      objrs1.Open strquery1,conn, 3,3
      While Not objrs1.eof
      prodid=objrs1(0 )
      prod_name=objrs 1(2)
      Response.Write "<option value='" & prodid & "' " & strSel1 & ">" &prod_name& "</option>"
      %> <!-- <option value="<%=prod_ id%>" selected><%=pro d_name%></option> -->
      <% objrs1.moveNext
      Wend
      </select>


      <SCRIPT LANGUAGE="JavaS cript">
      function reload(form)
      {
      var val=myform.selp rod.options[myform.selprod. options.selecte dIndex].value;
      document.myform .method="Post";
      document.myform .action="index. asp?page=list"& id="+ val
      document.myform .submit();
      }

      Thanks...

      Comment

      Working...