Populate Dropdown from SQL in Classic ASP - asp-classic

I am trying to force users to only select certain values when adding a record. So naturally I am using a dropdown, but I'd like the options to be populated by a specific field in the database. I figured I'd do a Do/Loop but I am apparently doing something wrong.
Dim dstrSQL
Dim drs
dstrSQL = "SELECT EventID FROM Events"
set conn2 = CreateObject("ADODB.Connection")
conn2.open CONN_STRING
set drs = conn2.execute(dstrSQL)
conn2.close: set conn2 = nothing
Do
Response.Write "<option value=" & drs.Fields(0) & " >" & drs.Fields(0) & "</option>"
drs.MoveNext
Loop

It's been a long time. Something like this:
conn2.open CONN_STRING
set drs = conn2.execute(dstrSQL)
do while not drs.eof %>
<option value="<%= drs.Fields(0) %>" ><%= drs.Fields(0) %></option>
<% drs.MoveNext
Loop
conn2.close
set conn2 = nothing %>

Related

How can I add/update recordset based on dynamic posted input fields? [duplicate]

on submit of a form, I would like to capture the field names and values of the forms and I want them passed without even showing in the browser (Response.Write makes them visible in the browser). How I can do this please? I am using this code:
For Each Item In Request.Form
fieldName = Item
fieldValue = Request.Form(Item)
Response.Write(""& fieldName &" = Request.Form("""& fieldName &""")")
Next
Your code is essentially correct, so just remove the Response.Write and do something else with the fieldName and fieldValue variables you're populating. After you're done with manipulating the data (either inserting it into a database or sending an e-mail), you can redirect the user to a success / thank you page.
To test that you're receiving the correct input, you can change your Response.Write to
Response.Write fieldName & " = " & fieldValue & "<br>"
Update
Here's how you could use a Dictionary Object to put your field names and field values together:
Dim Item, fieldName, fieldValue
Dim a, b, c, d
Set d = Server.CreateObject("Scripting.Dictionary")
For Each Item In Request.Form
fieldName = Item
fieldValue = Request.Form(Item)
d.Add fieldName, fieldValue
Next
' Rest of the code is for going through the Dictionary
a = d.Keys ' Field names '
b = d.Items ' Field values '
For c = 0 To d.Count - 1
Response.Write a(c) & " = " & b(c)
Response.Write "<br>"
Next
This is a very small snippet I use to show all form fields POSTED
<%
For x = 1 to Request.Form.Count
Response.Write x & ": " _
& Request.Form.Key(x) & "=" & Request.Form.Item(x) & "<BR>"
Next
%>
To complement the answer, and reply to the second question #ReneZammit asked in his comment from Jan 31, 2012 at 10:17, Creating "dynamic" ASP/VBScript variables based on the field names IS POSSIBLE!
You just have to use the "magical" Execute() function :
<%
'If the form was SUBMITTED
If (Request.Form("submitForm") = "1") then
Dim fieldName
Dim fieldValue
'Loop through all the form items
For Each Item In Request.Form
'Get Form item properties
fieldName = Item
fieldValue = Request.Form(Item)
'Use Execute() to interpret dynamically created ASP code
Execute("Dim myVar_" & fieldName)
Execute("myVar_" & fieldName & " = """ & fieldValue & """")
Next
'Use your new Dynamic variables Names, that are now REAL native variables names!
Response.Write "<br>myVar_banana = " & myVar_banana
Response.Write "<br>myVar_pear = " & myVar_pear
Response.Write "<br>myVar_tomato = " & myVar_tomato
'If the form have to be DISPLAYED
else
%>
<form method="post">
<input type="text" name="banana" value="This is Yellow">
<br>
<input type="text" name="pear" value="This may be Green">
<br>
<input type="text" name="tomato" value="This should be Red">
<input type="hidden" name="submitForm" value="1">
<input type="submit">
</form>
<%
end if
%>
More information available here : http://www.aspdev.org/asp/asp-eval-execute/

classic asp verify the server result

I am trying to check or unchecked the check-boxes depends upon the data results that comes from server. But I cannot use below code correctly where I am doing wrong?
<%
Dim AFTER_SAVE, IN_VIEW, Y
Dim SQL, Data
SQL = " SELECT code, name, value FROM mytable WHERE code = '" & User & "'"
Data = Data(SQL)
%>
<%If IsArray(Data) Then%>
<%If ((Data(1,0) = "AFTER_SAVE") AND (Data(2,0) = "Y")) Then %>
document.getElementById("chkSave").checked == true;
<%End If%>
<% If ((Data(1,0) = "IN_VIEW") AND (Data(2,0) = "Y")) Then %>
document.getElementById("chkVIEW").checked == true;
<%End If%>
<%End If%>
You're trying to combine server-side code with client-side code in a very strange way. Sometimes, it's necessary to do that (i.e. use server-side VBScript to write client-side Javascript), but if I'm understanding your intent correctly, it's not needed here.
Basically, if this is actually a classic ASP page, then somewhere on that page you're generating the checkboxes in question. So all you need to do is put your database call somewhere before that, and then when you generate the checkboxes, you can output a checked='checked', or not, depending.
Note that I have no clue what Data = Data(SQL) is supposed to mean. There's no way for it to be valid VBScript code - parentheses are for arrays, but a string is not a valid array index, and then to assign it to itself like that? Anyway, I'm ignoring that part.
<html>
<head>
<%
Dim AFTER_SAVE, IN_VIEW
Dim SQL, RS, Conn
Dim User
'...blah blah blah, give a value to User, set up your DB connection, etc. etc....
SQL = "SELECT code, name, [value] FROM mytable WHERE code = '" & User & "'"
'- ("value" is a reserved keyword in SQL, hence the brackets)
Set RS = Server.Createobject("ADODB.Recordset")
RS.Open SQL, Conn, 1, 2 '- this is rather handwavy and unlikely to actually
'- work as-is; use the options and connection methods that make sense for you
Do Until RS.EOF
'- I have no idea how your data is set up; this may make no sense.
'- The idea is, read the checkbox states from your database, and
'- stick them in variables for later reference.
Select Case RS("name")
Case "AFTER_SAVE" AFTER_SAVE = RS("value")
Case "IN_VIEW" IN_VIEW = RS("value")
End Select
RS.Movenext
Loop
RS.Close
Set RS = Nothing
%>
</head>
<body>
<form method="post" action="myformhandler.asp">
<!-- form fields and stuff -->
<input type="checkbox" name="chkSave" id="chkSave" <%
If AFTER_SAVE = "Y" Then Response.Write "checked='checked'"
%> value="Y"><label for="chkSave">After save</label>
<input type="checkbox" name="chkView" id="chkView" <%
If IN_VIEW = "Y" Then Response.Write "checked='checked'"
%> value="Y"><label for="chkView">In view</label>
<!-- more form stuff -->
</form>
</body>
</html>

little help on asp - response write

I set a variable :
Dim adoRecordset
and set it as:
Set adoRecordSet = Server.CreateObject("ADODB.RecordSet")
adoRecordset.Open mSQL , adoConnection , adOpenForwardOnly
and I use it into show my data from database
eg. 1. <td align="left"><%=adoRecordset("loc")%></td>
and I would like add a asp code "if" & "else"
but this : 2. Response.Write "<td adoRecordset(loc)></td>"
doesn't work.
How can I make asp code 2. work as 1. ?
My asp classic is rusty, but I think you are looking for something like:
<%
If adoRecordset("loc") <> "" Then
Response.Write(adoRecordset("loc"))
End If
%>
Or with a local var to cache the result:
<%
Dim someLoc
Set someLoc = adoRecordset("loc")
If someLoc <> "" Then
Response.Write("<td>" & someLoc & "</td>")
End If
%>
If you've got large amounts of conditional Html output, then you can switch out of server code again, like so:
<% If something or other Then %>
Some Conditional Html Here
<% Else %>
Else Html here
<% End If %>
<%= is shorthand to emit a value
whereas <% escapes to server side code.
Dim adoRecordset
Set adoRecordSet = Server.CreateObject("ADODB.RecordSet")
adoRecordset.Open mSQL , adoConnection , adOpenForwardOnly
if not adoRecordset.EOF then
do while not adoRecordSet.EOF
if adoRecordset("columnName") = "test value 1" then
response.write("<td>" & adoRecordset("columnName") & "</td>")
else
response.write("<td>I want to do something else here</td>")
end if
adoRecordset.movenext
loop
end if
adoRecordset.close
set adoRecordset = nothing

Looping through a form to get field names and filed values issue (classic ASP)

on submit of a form, I would like to capture the field names and values of the forms and I want them passed without even showing in the browser (Response.Write makes them visible in the browser). How I can do this please? I am using this code:
For Each Item In Request.Form
fieldName = Item
fieldValue = Request.Form(Item)
Response.Write(""& fieldName &" = Request.Form("""& fieldName &""")")
Next
Your code is essentially correct, so just remove the Response.Write and do something else with the fieldName and fieldValue variables you're populating. After you're done with manipulating the data (either inserting it into a database or sending an e-mail), you can redirect the user to a success / thank you page.
To test that you're receiving the correct input, you can change your Response.Write to
Response.Write fieldName & " = " & fieldValue & "<br>"
Update
Here's how you could use a Dictionary Object to put your field names and field values together:
Dim Item, fieldName, fieldValue
Dim a, b, c, d
Set d = Server.CreateObject("Scripting.Dictionary")
For Each Item In Request.Form
fieldName = Item
fieldValue = Request.Form(Item)
d.Add fieldName, fieldValue
Next
' Rest of the code is for going through the Dictionary
a = d.Keys ' Field names '
b = d.Items ' Field values '
For c = 0 To d.Count - 1
Response.Write a(c) & " = " & b(c)
Response.Write "<br>"
Next
This is a very small snippet I use to show all form fields POSTED
<%
For x = 1 to Request.Form.Count
Response.Write x & ": " _
& Request.Form.Key(x) & "=" & Request.Form.Item(x) & "<BR>"
Next
%>
To complement the answer, and reply to the second question #ReneZammit asked in his comment from Jan 31, 2012 at 10:17, Creating "dynamic" ASP/VBScript variables based on the field names IS POSSIBLE!
You just have to use the "magical" Execute() function :
<%
'If the form was SUBMITTED
If (Request.Form("submitForm") = "1") then
Dim fieldName
Dim fieldValue
'Loop through all the form items
For Each Item In Request.Form
'Get Form item properties
fieldName = Item
fieldValue = Request.Form(Item)
'Use Execute() to interpret dynamically created ASP code
Execute("Dim myVar_" & fieldName)
Execute("myVar_" & fieldName & " = """ & fieldValue & """")
Next
'Use your new Dynamic variables Names, that are now REAL native variables names!
Response.Write "<br>myVar_banana = " & myVar_banana
Response.Write "<br>myVar_pear = " & myVar_pear
Response.Write "<br>myVar_tomato = " & myVar_tomato
'If the form have to be DISPLAYED
else
%>
<form method="post">
<input type="text" name="banana" value="This is Yellow">
<br>
<input type="text" name="pear" value="This may be Green">
<br>
<input type="text" name="tomato" value="This should be Red">
<input type="hidden" name="submitForm" value="1">
<input type="submit">
</form>
<%
end if
%>
More information available here : http://www.aspdev.org/asp/asp-eval-execute/

How to display data from stored procedure in a drop down in Classic ASP?

This is related to Classic ASP code.
A page fetches data for a particular ProjectCode.
There is a general text field which shows the Site-Location for selected Project-Code. I want to change it to drop down, so that a user can change the Site-Location from options available (fetched from DB) and then save it. Also, on page load the Site-Location of Project-Code for that particular entry should be selected.
I have added following code to my Page, but it doesn't work(definately I am new to classic ASP).
strSQL = "SP_GET_SiteLocation"
Set rsSiteList = RunSQLQuery(strSQL)
'show the list
If Not rsSiteList.EOF Then
Do While NOT rs.EOF
SiteLocationList= SiteLocationList & "<option value="">" & rs("LOCATION") & "</option>"
rs.MoveNext
Also, on click of save button, i have to send the selected drop down value to update query.
You use a wrong name for the recordset variable..
You named it rsSiteList but you use it as rs
Do While NOT rsSiteList.EOF
SiteLocationList= SiteLocationList & "<option value="">" & rsSiteList("LOCATION") & "</option>"
rsSiteList.MoveNext
Update
You are building a string with all the options ..
you should write it in the page at some point.. response.write(SiteLocationList)
or write the <options> directly to the page..
<select name="somename"><%
Do While NOT rsSiteList.EOF
%>
<option value=""><%=rsSiteList("LOCATION")%></option>
<%
rsSiteList.MoveNext
Loop
%>
</select>
update 2
Not sure why you do not want to print the options as you read them from the recordset but prefer to make a huge string instead and print that at the end ... it is the same thing but much more cleaner ..
The following should select the location that matches the rsReqDetails("AppReqSiteID")
<td>
<%
strSQL="SP_EPAPM_GET_SiteLocation"
Set rsSiteList=RunSQLQuery(strSQL)
selectedValue = rsReqDetails("AppReqSiteID")
If Not rsSiteList.EOF Then
Do While NOT rsSiteList.EOF
loc = rsSiteList("LOCATION")
if loc <> selectedValue then
optionOpen = "<option>"
else
optionOpen = "<option selected=""selected"">"
end if
optionClose = "</option>"
SiteLocationList=SiteLocationList & optionOpen & rsSiteList("LOCATION") & optionClose
rsSiteList.MoveNext
Loop
End If
%>
<select id="SiteLocationList" NAME="SiteLocationList">
<%response.write(SiteLocationList)%>
</select>
</td>
In general you need to watch the nesting of html as it can mess everything up. Also you need to read a little on the interactions between ASP and HTML ...
You almost got it, you're missing the "select" tag:
strSQL = "SP_GET_SiteLocation"
Set rsSiteList = RunSQLQuery(strSQL)
'show the list
If Not rsSiteList.EOF Then %>
<select><%Do While NOT rs.EOF SiteLocationList= SiteLocationList & "<option value="">" & rs("LOCATION") & "</option>"
rs.MoveNext %>
</select>

Resources