Google Answers Logo
View Question
 
Q: ASP Submit Button Help ( No Answer,   4 Comments )
Question  
Subject: ASP Submit Button Help
Category: Computers > Programming
Asked by: callawar-ga
List Price: $75.00
Posted: 16 Sep 2005 05:40 PDT
Expires: 16 Oct 2005 05:40 PDT
Question ID: 568674
The following is the ASP page that was created.  What am I trying to
accomplish is to make the pull down menu "Approve/Deny"  update the
"Status" column that is displayed to the browser once the submit
"Update" button is selected at the bottom of the page. The "Status"
column will update to whatever is selected from the pull down menu,
"Approve, "Deny" or "Pending."  Additionally, the "Pending" status is
the default value in the "Status" column only until it is updated when
the submit "Update" button is selected.



 <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="includes/functions.inc.asp"-->
<%
	ConnectToDatabase("") ' Connect to the database
	Dim days ' Our variable to hold the # of days to display
	
	'If nothing is submitted from the user then set it to 60.
	If Request("Days") = "" Then
		days = 60
	Else
		days = CInt(Request("Days"))
	End If
	
	Dim approve, strMessage, strDelivery, strEmail, key
	strMessage = Request("Message") ' Store our display message
	bLogin = false ' Boolean variable to tell whether the user is logged in or not
	approve = false ' Boolean which grants permission to approve/deny requests
	
	' If they want to logout clear their cookies
	If Request("Logout") = 1 Then
		Response.Cookies("LoginType") = ""
		Response.Cookies("Unit") = ""
	End If
	
	' If a password has been submitted attempt to log the user in
	If Request("Password") <> "" Then
		' Select the unit they specified and check the password against it
		Query "SELECT * FROM Units WHERE ID = " & Request("Unit") & " AND
ApprovalPassword = '" & Request("Password") & "'", 0
		' If it validates then setup our login variables and store the
authentication cookies
		If NOT dbResult(0).EOF Then
			approve = true
			bLogin = true
			Response.Cookies("LoginType") = "approve"
			Response.Cookies("Unit") = dbResult(0)("ID")
		End If
		CloseQuery(0)
		
		' If we're still not logged in check them against the "view-only" password.
		If NOT bLogin Then
			Query "SELECT * FROM Units WHERE ID = " & Request("Unit") & " AND
ViewPassword = '" & Request("Password") & "'", 0
			' Setup their authentication but don't include the approve = true
			If NOT dbResult(0).EOF Then
				bLogin = true
				Response.Cookies("LoginType") = "view"
				Response.Cookies("Unit") = dbResult(0)("ID")
			End If
			CloseQuery(0)
		End If
		' If they're still not validated it's a bad password so let them know!
		If NOT bLogin Then
			strMessage = "The password you entered is invalid, please try again."
		End If
		' Redirect with the appropriate message
		Response.Redirect("viewall1.asp?Message=" & strMessage)	
	End If
	
	' Setup our authentication variables based on the cookies we stored above
	If Request.Cookies("LoginType") <> "" Then
		bLogin = true
		If Request.Cookies("LoginType") = "approve" Then
			approve = true
		End If
	End If
	
	If Request("Update") <> "" AND approve Then
		dbResult(0).CursorType=2
		dbResult(0).LockType=3
		Query "SELECT * FROM Requests WHERE EndDate >= #" & Now() - days &
"# AND Unit = " & Request("Unit") & " ORDER BY StartDate DESC, EndDate
DESC, LastName DESC", 0
		Do While NOT dbResult(0).EOF
			If Request("RECORD_" & dbResult(0)("Key")) <> "" Then
				' If the status has changed from whats in the database then update it
				If Request("RECORD_" & dbResult(0)("Key")) <> dbResult(0)("Status") Then
					dbResult(0)("Status") = Request("RECORD_" & dbResult(0)("Key"))
					dbResult(0)("DateApproved") = Now()
					dbResult(0).Update

					Dim objCDOMail	'Holds the CDONTS NewMail Object
					Set objCDOMail = Server.CreateObject("CDONTS.NewMail")
		
					strEmail = "Sir/Ma'am, your travel request is now " &
dbResult(0)("Status") & ". Please <a
href='http://source/applications/travel/view.asp?ID=" &
dbResult(0)("ID") & "'>click here</a> to view the request."
					objCDOMail.From 		= "IBM /Helpdesk <helpdesk@ibm.com>"
					objCDOMail.To 			= dbResult(0)("Email")
					objCDOMail.Subject 		= "Out of Town Travel Request - Status Update"
					objCDOMail.BodyFormat 	= 0
					objCDOMail.MailFormat 	= 0
					objCDOMail.Body 		= strEmail
					objCDOMail.Importance 	= 1
					objCDOMail.Value("Reply-To") = "ibm <helpdesk@ibm.com>"
					objCDOMail.Send
					Set objCDOMail = Nothing
				End If
			End If
			dbResult(0).MoveNext
		Loop
		CloseQuery(0)
		strMessage = "The requests have been updated."
		Response.Redirect("viewall1.asp?Message=" & strMessage)
	End If

	
	StartHTML("View All Requests")%>
	<center>
	<table border="0" cellpadding="1" cellspacing="1" class="content"
style="margin-left:5px">
	<% 
	' If they aren't logged in display the below login form
	If NOT bLogin Then %>
	<form action="viewall1.asp" method="post">
	<tr>
		<td class="head_big" align="center" colspan="3">&nbsp;&nbsp;39 ABW
Travel Request&nbsp;&nbsp;</td>
	</tr>
	<tr>
		<td class="text">&nbsp;Unit:&nbsp;&nbsp;</td>
		<td class="text">&nbsp;<select name="Unit" size="1">
	<%
		Query "SELECT * FROM Units ORDER BY Name ASC", 0
		Do While NOT dbResult(0).EOF
			%>
			<option value="<%=dbResult(0)("ID")%>"><%=dbResult(0)("Name")%></a></option>
			<%
			dbResult(0).MoveNext
		Loop
		CloseQuery(0)%>
	</select>&nbsp;&nbsp;</td>
	</tr>
	<tr>
		<td class="text">&nbsp;Password:&nbsp;&nbsp;</td>
		<td class="text">&nbsp;<input type="password" name="password"
value="">&nbsp;&nbsp;</td>
	</tr>
	<% If strMessage <> "" Then %>
	<tr>
		<td class="text" colspan="2"><%=strMessage%></td>
	</tr>
	<% End If%>
	<tr>
		<td class="head" align="center" colspan="2"><input type="submit"
name="submit" value="Submit"></td>
	</tr>
	</form>
	<% 
	' End of the login form
	Else 
	' Display below if they are logged in
	%>
	<% Query "SELECT * FROM Units WHERE ID = " & Request("Unit"), 1 %>
	<form action="viewall1.asp" method="post">
	<input type="hidden" name="Days" value="<%=days%>">
	<input type="hidden" name="Unit" value="<%=Request("Unit")%>">
	<tr>
		<td class="head_big" align="center"
colspan="5">&nbsp;&nbsp;<%=dbResult(1)("Name")%> Travel
Requests&nbsp;&nbsp;</td>
	</tr>
	<% CloseQuery(1) %>
	<tr>
		<td class="subHead">&nbsp;User&nbsp;&nbsp;</td>
		<td class="subHead">&nbsp;Travel Dates&nbsp;&nbsp;</td>
		<td class="subHead">&nbsp;Date Requested&nbsp;&nbsp;</td>
		<td class="subHead">&nbsp;Status&nbsp;&nbsp;</td>
	<%	If approve Then %>
		<td class="subHead">&nbsp;Approve/Deny&nbsp;&nbsp;</td>
	<%	Else %>
		<td class="subHead" width="0">&nbsp;</td>
	<%	End If %>
	</tr>
	<%
	' Select the requests that are within the number of days setup above.	
	Query "SELECT * FROM Requests WHERE EndDate >= #" & Now() - days & "#
AND Unit = " & Request("Unit") & " ORDER BY StartDate DESC, EndDate
DESC, LastName DESC", 0
	' Loop through and display the requests
	Do While NOT dbResult(0).EOF
		' If the user has approval authority then display this
		If approve Then
		%>
	<tr>
		<td class="text" nowrap>&nbsp;<a
href="approve.asp?UID=<%=dbResult(0)("Key")%>"><%=dbResult(0)("LastName")%>,
<%=dbResult(0)("FirstName")%>&nbsp;<%=dbResult(0)("Rank")%></a>&nbsp;&nbsp;</td>
		<td class="text" nowrap>&nbsp;<%=dbResult(0)("StartDate")%> -
<%=dbResult(0)("EndDate")%>&nbsp;&nbsp;</td>
		<td class="text" nowrap>&nbsp;<%=dbResult(0)("DateSubmitted")%>&nbsp;&nbsp;</td>
		<td class="text" nowrap>&nbsp;<%=PrintStatus(dbResult(0)("Status"))%>&nbsp;&nbsp;</td>
		<td class="text" nowrap>&nbsp;<select
name="RECORD_<%=dbResult(0)("ID")%>" size="1">
			<option value="Pending"<% If dbResult(0)("Status") = "Pending" Then
%> selected<% End If %>>Pending</option>
			<option value="Approved"<% If dbResult(0)("Status") = "Approved"
Then %> selected<% End If %>>Approved</option>
			<option value="Denied"<% If dbResult(0)("Status") = "Denied" Then
%> selected<% End If %>>Denied</option>
			</select>&nbsp;</td>
	</tr>
		<%
		' Otherwise display the view only mode
		Else
		%>
	<tr>
		<td class="text" nowrap>&nbsp;<a
href="view.asp?ID=<%=dbResult(0)("ID")%>"><%=dbResult(0)("LastName")%>,
<%=dbResult(0)("FirstName")%>&nbsp;<%=dbResult(0)("Rank")%></a>&nbsp;&nbsp;</td>
		<td class="text" nowrap>&nbsp;<%=dbResult(0)("StartDate")%> -
<%=dbResult(0)("EndDate")%>&nbsp;&nbsp;</td>
		<td class="text" nowrap>&nbsp;<%=dbResult(0)("DateSubmitted")%>&nbsp;&nbsp;</td>
		<td class="text" nowrap>&nbsp;<%=PrintStatus(dbResult(0)("Status"))%>&nbsp;&nbsp;</td>
				<td class="text" nowrap>&nbsp;<select
name="RECORD_<%=dbResult(0)("ID")%>" size="1">
			<option value="Pending"<% If dbResult(0)("Status") = "Pending" Then
%> selected<% End If %>>Pending</option>
			<option value="Approved"<% If dbResult(0)("Status") = "Approved"
Then %> selected<% End If %>>Approved</option>
			<option value="Denied"<% If dbResult(0)("Status") = "Denied" Then
%> selected<% End If %>>Denied</option>
			</select
	></tr>
		<%
		End If
		dbResult(0).MoveNext
	Loop
	CloseQuery(0)
	If approve Then
	%>
	<tr>
		<td class="text" colspan="4">&nbsp;</td>
		<td class="head" align="center"><input type="submit" name="Update"
value="Update"></td>
	</tr> 
	</form>
		<form action="viewall1.asp" method="get" name="switch">
		<tr>
			<td class="subHead" align="center" colspan="5">View From Last:
<select name="Days" size="1"
onChange="javascript:document.forms['switch'].submit();">
			<option value="14"></option>
			<option value="14"<% If Request("Days") = 14 Then %> selected<% End
If%>>2 Weeks</option>
			<option value="30"<% If Request("Days") = 30 Then %> selected<% End
If%>>30 Days</option>
			<option value="60"<% If Request("Days") = 60 Then %> selected<% End
If%>>60 Days</option>
			<option value="90"<% If Request("Days") = 90 Then %> selected<% End
If%>>90 Days</option>
			<option value="180"<% If Request("Days") = 180 Then %> selected<%
End If%>>Six Months</option>
			<option value="365"<% If Request("Days") = 365 Then %> selected<%
End If%>>Year</option>
			</select></td>
		</tr>
		</form>
	<% End If %>
	<% End If %>
	</table>
	</center>
	<%EndHTML()%>

Clarification of Question by callawar-ga on 21 Sep 2005 08:15 PDT
What I am trying to accomplish with this script is when the "Update"
button is pressed, the option values of "Pending" or "Denied" or
"Approved" are entered into the database.  What I am having trouble
with is setting up the response.write script to update the "Status"
field.  Any suggestions are welcome.  Thanks.

<tr>
		<td class="text" nowrap>&nbsp;<a
href="view.asp?ID=<%=dbResult(0)("ID")%>"><%=dbResult(0)("LastName")%>,
<%=dbResult(0)("FirstName")%>&nbsp;<%=dbResult(0)("Rank")%></a>&nbsp;&nbsp;</td>
		<td class="text" nowrap>&nbsp;<%=dbResult(0)("StartDate")%> -
<%=dbResult(0)("EndDate")%>&nbsp;&nbsp;</td>
		<td class="text" nowrap>&nbsp;<%=dbResult(0)("DateSubmitted")%>&nbsp;&nbsp;</td>
		<td class="text" nowrap>&nbsp;<%=PrintStatus(dbResult(0)("Status"))%>&nbsp;&nbsp;</td>
		<td class="text" nowrap>&nbsp;<select
name="RECORD_<%=dbResult(0)("ID")%>" size="1">
			<option value="Pending"<% If dbResult(0)("Status") = "Pending" Then
%> selected<% End If %>>Pending</option>
			<option value="Approved"<% If dbResult(0)("Status") = "Approved"
Then %> selected<% End If %>>Approved</option>
			<option value="Denied"<% If dbResult(0)("Status") = "Denied" Then
%> selected<% End If %>>Denied</option>
			</select>&nbsp;</td>
			</tr>
		<%
		End If
		dbResult(0).MoveNext
	Loop
	CloseQuery(0)
	If approve Then
	%>
	
	<tr>
		<td class="text" colspan="4">&nbsp;</td>
		<td class="head" align="center"><input type="submit" name="Update"
value="Update"></td>
Answer  
There is no answer at this time.

Comments  
Subject: Re: ASP Submit Button Help
From: jeffemminger-ga on 16 Sep 2005 12:50 PDT
 
here's a proof of concept on how to iterate the Form collection and
pull out fields matching a naming convention ("RECORD_X") and their
corresponding values:

<%
	dim sKey, sVal, record_id
	
	if (lcase(request.servervariables("request_method")) = "post") then
		
		for x = 1 to request.form.count
			sKey = request.form.key(x)
			
			if (left(sKey, 7) = "RECORD_") then
				record_id = split(sKey, "_")(1)
				sVal = request.form(x)
				
				rem do your update for this record_id here
				response.write("<div>")
				response.write("update record " & record_id & ": " & sVal)
				response.write("</div>")
			end if
			
		next
		
	end if
%>
<form method="post">
	<%
		for x = 1 to 10
			response.write("<div>")
			response.write(x & ". ")
			response.write("<select name='RECORD_" & x & "'>")
			response.write("<option>Pending</option><option>Approved</option><option>Denied</option>")
			response.write("</select>")
			response.write("</div>")
		next
	%>
	<div><input type="submit" value="update" /></div>
</form>
Subject: Re: ASP Submit Button Help
From: callawar-ga on 16 Sep 2005 13:49 PDT
 
Thanks for the input.  I will check it out.
Subject: Re: ASP Submit Button Help
From: jeffemminger-ga on 21 Sep 2005 11:02 PDT
 
right, so in my sample code where it says
 rem do your update for this record_id here

you would issue your sql statements for updating those records' Status, e.g.

 rem do your update for this record_id here
 sql = "update myTable set Status = '" & sVal & "' where record_id = " & record_id

this would need to happen before you attempt to SELECT the records
again for display... then your existing logic for selecting the
current Status would take care of selecting the appropriate option in
each select list.

does this make sense?  is this what you are trying to do?
Subject: Re: ASP Submit Button Help
From: jeffemminger-ga on 21 Sep 2005 11:43 PDT
 
ok, i see where it looks like you already have some code to perform
your updates.  is the problem that this code is not performing the
update?

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy