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

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/

Related

ASP Radio Button Null Value

I have an "edit" form that is pulling data from a specific row. Part of this form includes radio buttons (a set of four). I am able to retrieve the data from the radio button that has been selected but the other three don't have anything and I get a Null Value error. How could I prevent this? I essentially have 1 cell that pushes in the value of the radio button that was selected. In my asp code I have it set up like this:
<input <%If (CStr((rsCourseNum.Fields.Item("question1Correct").Value)) = CStr("answer1")) Then Response.Write("checked=""checked""") : Response.Write("")%> type="radio" name="question1Correct" id="question1Correct" value="answer1">
this would throw an error if answer0 was in the db since there is no answer1, I'm just not sure exactly how to set this up to prevent it from calling a null value.
What's the Response.Write("") for?
You're not getting an error because you're checking a db value that happens to be Null; you're getting an error because you're trying to convert a Null to a string. There are two* ways around this. Method one is to not do any data type conversions:
Response.Write "<input type=""radio"" name=""question1Correct"" id=""q1c"""
If rsCourseNum("question1Correct") = "answer1" Then Response.Write " checked"
Response.Write " value=""answer1""><label for=""q1c"">Question 1</label>"
This will work with Nulls because the comparison Null = "answer1" will return Null, which isn't True, so the button isn't marked.
The other method is to explicitly check for Nulls, and only do a data type conversion if the value isn't null.
Response.Write "<input type='radio' name='q" & i & "' id='q" & i & "c'"
If Not IsNull(rs("q" & i)) Then
If CStr(rs("q" & i)) = CStr(answers(i,0)) Then Response.Write " checked"
End If
Response.Write " value='" & answers(i,0) & "'>"
Response.Write "<label for='q" & i & "c'>" & answers(i,1) & "</label>"
* Well, two, uh, "proper" ways around this. There's also the hacky way: append a blank string instead of using CStr. (Thanks for the tip, Lankymart!)
If rs("q" & i) & "" = CStr(answers(i,0)) Then Response.Write " checked"

Comma Separated String into a drop down in classic asp

I have a comma separated string
say 12345,67890,3453,124556,56778
and I want to display these items in a drop down list
I am working in a classic asp page.
Please help me on this
Give this a try:
<%
' put your string into a variable.
Dim myString : myString = "12345,67890,3453,124556,56778"
' split your variable up into a array
Dim splitmystring : splitmystring = split(myString,",")
' create a dropdown box
Response.write "<select value=""dropdown"">"
Response.write "<option selected>choose a option</option>"
' Loop through your array
For Each item In splitmystring
response.write "<option value='"& item &"'>"& item & "</option>"
Next
'close your dropdown box
response.write "</select>"
%>

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>

Populate Dropdown from SQL in Classic ASP

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 %>

Resources