Search code examples
javascriptvbscriptasp-classic

Classic ASP: How to pass the selected value from Dropdown to SELECT query on onchange event


I need to pass the selected value from dropdownlist to the SELECT query and based on the value passed the respective table/recordset displayed in the page. I'm struggling to find way to how to pass the value.

 function getUnitCode() {    
    var x = document.getElementById("UnitCodeDDL");    
    var unical = x.options[x.selectedIndex].value;
    alert(unical);
}
<%   
   Dim strUnitCode
   strUnitCode=Request.Form("UnitCodeDDL") 
 %>
 <SELECT NAME=UnitCodeDDL id=UnitCodeDDL onchange="getUnitCode();javascript: document.forms['VehForm'].submit()">
    <OPTION VALUE=''>Please Select UnitCode</OPTION>
<%
 Dim objRS , strSQL
  Set objRS=Server.CreateObject ("ADODB.Recordset")
         
  strSQL = "SELECT v.unitcode, u.description "
  strSQL = strSQL & "FROM " & strBrand & "_vehicle AS v " 
  strSQL = strSQL & "LEFT JOIN unipos AS u "
  strSQL = strSQL & "ON v.unitcode = u.unitcode "
  strSQL = strSQL & "WHERE v_code='" & strVCode & "' "
  strSQL = strSQL & "GROUP BY v.unitcode;"
         
  objRS.Open strSQL,objConn
  Do While Not objRS.EOF
     Response.Write "<OPTION VALUE='" & objRS("unitcode") & "'"
     if  objRS("unitcode")=strUnitCode Then Response.Write " selected " 
     Response.Write ">"
     Response.Write objRS("unitcode") & "</OPTION>"
     objRS.MoveNext
  Loop
  objRS.Close
  Set objRS=Nothing
  Response.Write "</SELECT>"
%>
 <% 
  
  Dim strAutoSection 
  strAutoSection = strAutoSection & "<FORM name='VehForm' METHOD=POST ACTION='selectVeh.asp?brand=" & strBrand & "&VCode=" & strVCode & "&Veh=" & server.htmlencode(strVeh) & "&action=upload' enctype='multipart/form-data'>" & vbCrLf
  strAutoSection = strAutoSection & "Upload file:<INPUT TYPE='file' NAME='uploadFile'><input type='submit' value='upload'></FORM>"
  
  strAutoSection = strAutoSection & "<TABLE border=1 cellspacing=1 cellpadding=2>" & vbCrLf
  strAutoSection = strAutoSection & "<TR bgcolor='#CCFFCC'>" & vbCrLf
  strAutoSection = strAutoSection & "<TH>UNITCODE</TH>" & vbCrLf
  strAutoSection = strAutoSection & "<TH>DESCRIPTION</TH>" & vbCrLf  
  strAutoSection = strAutoSection & "</TR>" & vbCrLf

  Dim objRS_auto,strSQL
  Set objRS_auto=Server.CreateObject ("ADODB.Recordset")
  strSQL = "SELECT v.*, u.description "
  strSQL = strSQL & "FROM " & strBrand & "_vehicle AS v "
  strSQL = strSQL & "LEFT JOIN unitpos AS u "
  strSQL = strSQL & "ON v.unitcode = u.unitcode  "
  strSQL = strSQL & "WHERE v_code='" & strVCode & "' AND v.unitcode='" & strUnitCode & "' "
  objRS_auto.Open strSQL, objConn
%>

All I have to get the value from UnitCodeDDL on onchange event and pass the same to strUnitCode variable in the query. Can anyone please guide me how to solve this?


Solution

  • I suspect you are looking the in request.form object but not stipulating method=post in your form tag (which you havent shown)

    <form method=post>
        <select name=myselect>....
        </select>
    <form>
        
    <% val = request.form("myselect") %>
    

    as an aside you might want to use line continuations in your code and having the spaces at the start of each line makes it more readable. You also might need to consider the dangers of SQL Injection unless you want your entire database stolen or deleted!

       strSQL = " SELECT v.unitcode, u.description" & _
                " FROM " & strBrand & "_vehicle AS v" & _
                " LEFT JOIN unipos AS u" & _
                " ON v.unitcode = u.unitcode" & _
                " WHERE v_code='" & strVCode & "'" & _
                " GROUP BY v.unitcode;"