
July 23rd, 2007, 06:59 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Netherlands
|
|
|
Dynamic Dropdowns
Since I just wrote this for a member in this thread, I reckoned I might just as well post it here.
Code:
<script type="text/javascript">
function RefreshForm()
{
document.getElementById("SubmitType").value = "Refresh"
document.forms[0].submit();
}
function SubmitForm()
{
document.getElementById("SubmitType").value = "Submit"
document.forms[0].submit();
}
</script>
<form action="<%=Request.ServerVariables("SCRIPT_NAME")%>" method="POST">
<input type="hidden" name="SubmitType" id="SubmitType" />
<%
' Check for form submit
If Request.Form("SubmitType") = "Refresh" Then Call RefreshForm()
If Request.Form("SubmitType") = "Submit" Then Call HandleSubmit()
%>
<input type="button" value="Submit form" onclick="SubmitForm()" />
</form>
<%
' Subs and functions
Sub HandleSubmit()
'Handle input e.g. save to db
End Sub
Sub RefreshForm()
'Retrieve input
Dim First, Second, Third
First = Request.Form("First")
Second = Request.Form("Second")
Third = Request.Form("Third")
' Open the DB Conn...
Dim Conn, strDbConn
strDbConn = "YourConnString"
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open strDbConn
' Print out dropdowns
Call PrintDropDown "First", " SELECT ID, Label FROM FirstTable ", First ' Print first dropdown
If First <> "" Then Call PrintDropDown "Second", " SELECT ID, Label FROM SecondTable ", Second ' If first selection made print next dropdown
If Second <> "" Then Call PrintDropDown "Third", " SELECT ID, Label FROM ThirdTable ", Third ' If second selection made print next dropdown
'Close DB Conn
Conn.Close
Set Conn = Nothing
End Sub
Sub PrintDropDown(Name, SQL, SelectedValue)
Dim RS
Set RS = Conn.Execute(SQL)
If Not RS.EOF Then
%>select name="<%=Name%>" onChange="RefreshForm()"><%
Do While Not RS.EOF
%><option value="<%=RS(0)%>" <%=GetSelected(SelectedValue, RS(0) )%>><%=RS(1)%></option><%
RS.MoveNext
Loop
%></select><%
End If
Set RS = Nothing
End Sub
Function GetSelected(SelectedID, IDtoCheck)
If Cstr(SelectedID) = Cstr(IDtoCheck) Then
GetSelected = "selected=""selected"""
Else
GetSelected = ""
End If
End Function
%>
Hope this helps someone,
Tim
P.S. Haven't really tested it, If you find any errors, let me know I'll fix them.
|