I'll try and explain in detail how this is supposed to work:
I have a webpage with 2 links on it. Both links load the same .asp page but use a tablename session variable to decide what it will display. If the tablename is set to area, it loads all the area information from the database and displays it on the page. If it's set to region, it loads all the region information and displays it on the page.
On that page is a form, and any of the displayed information is editable. So when you hit submit, it submits the form with all the edited information to a second ASP page that compares the values to what's already in a database. If a value's already in there, it adds it to an array session value that passes them back to the first page and they're then displayed in an alert to the user.
That all works fine but once the session variable is set and the alert box appears, it will continue to appear every time you refresh the page with whichever values were last assigned to it. And also, if you move from the area - region portion of the page or region - area, it'll display the alert with the session variables from the other table as you move back and forth. They don't get updated until you hit submit again. Is there any way to have the session cleared once that page is done loading and the alert is popped?
Here's my code:
client side
Code:
<!--#include file="../strConnect.asp"#-->
<% Session.CodePage = 65001 ' UTF-8 code %>
<%
' Dim Variables
Dim fieldnames
Dim fld, fldCount, ColorOn, Sortkey
Dim Con, rs_Users, x, strkey, mySQL
Set Con = Server.CreateObject( "ADODB.Connection" )
' Pick up any Passed Variables
if Request.QueryString("table") <> "" then
Session("mytable") = Request.QueryString("table")
end if
if Request.QueryString("sort") <> "" then
sortkey = Request.QueryString("sort")
end if
%>
<HTML>
<HEAD>
<title>Location Management - Master Table Info</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<STYLE TYPE="text/css">
TD.large { font-size: 10pt; text-transform: uppercase;}
TD.small { font-size: 8pt;}
tr.blue {background-color: lightblue}
tr.white {background-color: white}
tr.titles {font-family: 'Verdana'; font-style: bold; font-weight: bold; background-color: lightblue}
input.Inputblue { font-size: 8pt; border: 1; background-color: lightblue}
input.Inputwhite { font-size: 8pt; border: 1; background-color: white}
input.borderon { font-size: 8pt; border: 1; background-color: lightblue}
</STYLE>
</HEAD>
<BODY topmargin="0" leftmargin="0" text="#4F2403" link="#4F2403" vlink="#4F2403" alink="#4F2403" rightMargin=0 bottomMargin=0>
<!--#include file="../top.asp"-->
<H1><% = Session("mytable") %> Information </H1>
<form method="post" Name=frmUser action="masupdate.asp">
<TABLE Border=0 RULES="Rows" CELLSPACING=0 CELLPADDING=2>
<TR Class=titles>
<%
Dim errorArray, errorCount, errorString
redim errorArray(10)
errorCount = Session("errorCount")
errorArray = Session("errorArray")
If errorCount > 0 Then
For x = 0 to errorCount - 1
errorString = errorString & "\r- " & errorArray(x)
Next
Response.write("<script type=""text/javascript"">alert(""The following values are invalid and were not committed: \r" & errorString & "\r\r> Each Manager can only be associated with a single area/region.\r> Each associated ID must be a valid Regional Manager ID.""); </script>")
End If
session("searchuser")=""
ColorOn = False
Con.Open strConnect
Select Case ucase(Session("myTable"))
Case "REGION_LIST"
mySQL = "SELECT * FROM " & Session("myTable") & " WHERE Region_ID NOT LIKE 'JR%';"
Case "DISTRICT_LIST"
mySQL = "SELECT * FROM " & Session("myTable") & ";"
Case "AREA_LIST"
mySQL = "SELECT * FROM " & Session("myTable") & ";"
Case Else
mySQL = "SELECT * FROM " & Session("myTable") & ";"
End Select
Set rs_Users = Con.Execute( mySQL )
If Not Rs_Users.EOF then
fldCount = rs_Users.Fields.Count
ReDim fieldnames(fldcount)
For x = 0 to fldCount - 1
Set fld = rs_Users.Fields.item(x)
fieldnames(x) = fld.Name
If x < 1 then
strkey = fld.Name
If sortkey = "" then
sortkey=strkey
End If
End if
HeaderLine = "<TD Class=Large>" & fld.Name & " <A TITLE=" & Chr(34) & "Sort By " & fld.Name & Chr(34) &_
" HREF=maslist.asp?table=" & Session("mytable") & "&Sort=" & fld.Name & ">"
If sortkey = fld.Name then
HeaderLine = HeaderLine & "<IMG BORDER=0 SRC=Desc.gif></A> </TD>"
Else
HeaderLine = HeaderLine & "<IMG BORDER=0 SRC=Sort.gif></A> </TD>"
End If
Response.Write HeaderLine
Next
End if
x = 0
rs_Users.Close
Select Case ucase(Session("myTable"))
Case "REGION_LIST"
mySQL = "SELECT * FROM " & Session("myTable") & " WHERE Region_ID NOT LIKE 'J%'" & " ORDER BY " & sortkey & ";"
Case "DISTRICT_LIST"
mySQL = "SELECT * FROM " & Session("myTable") & " WHERE District_ID NOT LIKE 'J%'" & " ORDER BY " & sortkey & ";"
Case "AREA_LIST"
mySQL = "SELECT * FROM " & Session("myTable") & " WHERE Area_ID NOT LIKE 'J%'" & " ORDER BY " & sortkey & ";"
Case Else
mySQL = "SELECT * FROM " & Session("myTable") & " ORDER BY " & sortkey & ";"
End Select
Set rs_Users = Con.Execute( mySQL )
Count = 0
%>
<TD><Input CLASS=borderon TYPE=TEXT SIZE=1 MAXLENGTH=1></INPUT></TD>
</TR>
<%
If not rs_Users.EOF then
rs_Users.movefirst
Do While NOT rs_Users.EOF
Count = Count + 1
If ColorOn = False then
Response.Write "<TR Class=white>"
For x = 0 to fldCount - 1
Response.Write "<TD Class=small><INPUT NAME=" & CHR(34) & fieldnames(x) & Count & CHR(34) & " ONCHANGE=vbscript:Document.frmUser.CHANGED" & Count & ".Value=" & CHR(34) & "T" & CHR(34) & " TYPE=TEXT CLASS=INPUTWHITE VALUE=" & CHR(34) & trim(rs_Users.Fields.item(x)) & CHR(34) & "></INPUT></TD>" & vbCRLF
Next
Response.Write "<TD><Input NAME=CHANGED" & Count & " CLASS=INPUTWHITE TYPE=HIDDEN SIZE=1 MAXLENGTH=1></INPUT></TD>" & vbCRLF & vbCRLF
ColorOn = True
Else
Response.Write "<TR Class=blue>"
For x = 0 to fldCount - 1
Response.Write "<TD Class=small><INPUT NAME=" & CHR(34) & fieldnames(x) & Count & CHR(34) & " ONCHANGE=vbscript:Document.frmUser.CHANGED" & Count & ".Value=" & CHR(34) & "T" & CHR(34) & " TYPE=TEXT CLASS=INPUTBLUE VALUE=" & CHR(34) & trim(rs_Users.Fields.item(x)) & CHR(34) & "></INPUT></TD>" & vbCRLF
Next
Response.Write "<TD><Input NAME=CHANGED" & Count & " CLASS=INPUTBLUE TYPE=HIDDEN SIZE=1 MAXLENGTH=1></INPUT></TD>" & vbCRLF & vbCRLF
ColorOn = False
End If
Response.Write "</TR>"
rs_Users.Movenext
Loop
End if
%>
</TABLE>
<% Response.Write "<INPUT type=""hidden"" name=""RowCount"" Value=""" & Count & """><INPUT type=""hidden"" name=""TableName"" Value=""" & Session("mytable") & """><BR>" %>
<BR>
<input type="hidden" width=50 name="table" value="<% =Session("mytable") %>"><BR>
<input type="button" value="<- Back" onClick="SureMain('master.asp')" id=button1 name=button1>
<input type="submit" value="Update" id=submit1 name=submit1>
<input type="button" value="Add New" onClick="SureMain('masnew.asp?Table=<%=Session("myTable")%>')" id=button1 name=button1>
</Form>
<%
Session("mytable") = ""
strkey = ""
rs_Users.close
Con.Close
Set rs_Users = nothing
%>
<!--#include file="../bottom.asp"-->
<script language="vbscript">
Function SureMain(strloc)
document.location = strloc
End Function
</script>
server side
Code:
<!--#include file="../strConnect.asp"#-->
<%
Dim Con, mySQL, rs_Users, rsupdate, sqlupdate, fldCount, myfield, savewhere, updatetype, mySQL2, rs_users2, rs_users3, mySQL3, mySQL4, mySQL5, rs_Users4, rs_Users5, mySQL6, rs_Users6, mySQL7, rs_Users7, mySQL8, rs_Users8, mySQL11, rs_Users11, mySQL12, rs_Users12
Dim RowCount
Dim errorArray(10), errorCount
Set Con = Server.CreateObject( "ADODB.Connection" )
Con.Open strConnect
updatetype = trim(request("type"))
tablename = Trim(Request.Form("Tablename"))
RowCount = Trim(Request.Form("RowCount"))
Response.Write RowCount & TableName
errorCount = 0
For x = 1 to RowCount
If Trim(Request.Form("CHANGED" & x) = "T") Then
if TableName = "CJI_AreaMgrs" Then
mySQL6 = "SELECT * FROM CJI_RegionMgrs WHERE Regional_Mgr_ID = '" & Trim(Request.Form("Regional_Mgr_ID" & x)) & "';"
response.write mysql6
Set rs_Users6 = Con.Execute( mySQL6 )
mySQL2 = "SELECT * FROM CJI_AreaMgrs WHERE Area_Mgr = '" & Trim(Request.Form("Area_Mgr" & x)) & "';"
response.write mysql2
Set rs_Users2 = Con.Execute( mySQL2 )
mySQL3 = "SELECT * FROM CJI_AreaMgrs WHERE Manage_Area_Name = '" & Trim(Request.Form("Manage_Area_Name" & x)) & "';"
response.write mysql3
Set rs_Users3 = Con.Execute( mySQL3 )
mySQL11 = "SELECT * FROM CJI_AreaMgrs WHERE Area_MgrJP = '" & Trim(Request.Form("Area_MgrJP" & x)) & "';"
response.write mysql11
Set rs_Users11 = Con.Execute( mySQL11 )
mySQL12 = "SELECT * FROM CJI_RegionMgrs WHERE Regional_Mgr_Name_JP = '" & Trim(Request.Form("Regoinal_Mgr_Name_JP" & x)) & "';"
Response.Write mySQL12
Set rs_Users12 = Con.Execute( mySQL12 )
if NOT rs_Users12.EOF AND Trim(Request.Form("Regional_Mgr_Name_JP" & x)) <> "" Then
errorArray(errorCount) = Trim(Request.Form("Regional_Mgr_Name_JP" & x))
errorCount = errorCount + 1
else
Response.Write x & " = has been changed. <br><br>"
Call updateRegionalManagerJapanese()
End If
if NOT rs_Users11.EOF AND Trim(Request.Form("Area_MgrJP" & x)) <> "" Then
errorArray(errorCount) = Trim(Request.Form("Area_MgrJP" & x))
errorCount = errorCount + 1
else
Response.Write x & " = has been changed. <br><br>"
Call updateAreaManagerJapanese()
End If
If rs_Users6.EOF AND Trim(Request.Form("Regional_Mgr_ID" & x)) <> "" Then
errorArray(errorCount) = Trim(Request.Form("Regional_Mgr_ID" & x))
errorCount = errorCount + 1
else
Response.Write x & " - Has been changed <BR><BR>"
Call updateID()
End If
If Not rs_Users2.EOF Then
mySQL7 = "SELECT Area_ID FROM CJI_AreaMgrs WHERE Area_Mgr = '" & Trim(Request.Form("Area_Mgr" & x)) & "' AND Area_ID != '" & Trim(Request.Form("Area_ID" & x)) & "';"
Set rs_Users7 = Con.Execute( mySQL7 )
if NOT rs_Users7.EOF AND Trim(Request.Form("Area_Mgr" & x)) <> "" Then
errorArray(errorCount) = Trim(Request.Form("Area_Mgr" & x))
errorCount = errorCount + 1
End If
else
Response.Write x & " - Has been changed <BR><BR>"
Call updateAreaManager()
End If
If Not rs_Users3.EOF Then
mySQL8 = "SELECT Manage_Area_Name FROM CJI_AreaMgrs WHERE Manage_Area_Name = '" & Trim(Request.Form("Manage_Area_Name" & x)) & "' AND Area_ID != '" & Trim(Request.Form("Area_ID" & x)) & "';"
Set rs_Users8 = Con.Execute( mySQL8 )
if NOT rs_Users8.EOF AND Trim(Request.Form("Manage_Area_Name" & x)) <> "" Then
errorArray(errorCount) = Trim(Request.Form("Manage_Area_Name" & x))
errorCount = errorCount + 1
End If
else
Response.write x & " - Has been changed <BR><BR>"
Call updateArea()
End If
elseIf TableName = "CJI_RegionMgrs" Then
mySQL4 = "SELECT * FROM CJI_RegionMgrs WHERE Regional_Mgr_Name = '" & Trim(Request.Form("Regional_Mgr_Name" & x)) & "';"
response.write mysql4
Set rs_Users4 = Con.Execute( mySQL4 )
mySQL5 = "SELECT * FROM CJI_RegionMgrs WHERE Management_Region_Name = '" & Trim(Request.Form("Management_Region_Name" & x)) & "';"
response.write mysql5
Set rs_Users5 = Con.Execute( mySQL5 )
If Not rs_Users4.EOF Then
mySQL10 = "SELECT Regional_Mgr_Name FROM CJI_RegionMgrs WHERE Regional_Mgr_Name = '" & Trim(Request.Form("Regional_Mgr_Name" & x)) & "' AND Regional_Mgr_ID != '" & Trim(Request.Form("Regional_Mgr_ID" & x)) & "';"
Set rs_Users10 = Con.Execute( mySQL10 )
if NOT rs_Users10.EOF AND Trim(Request.Form("Management_Region_Name" & x)) <> "" Then
errorArray(errorCount) = Trim(Request.Form("Regional_Mgr_Name" & x))
errorCount = errorCount + 1
End If
else
Response.Write x & " - Has been changed <BR><BR>"
Call updateRegionalManager()
End If
If Not rs_Users5.EOF Then
mySQL9 = "SELECT Management_Region_Name FROM CJI_RegionMgrs WHERE Management_Region_Name = '" & Trim(Request.Form("Management_Region_Name" & x)) & "' AND Regional_Mgr_ID != '" & Trim(Request.Form("Regional_Mgr_ID" & x)) & "';"
Set rs_Users9 = Con.Execute( mySQL9 )
if NOT rs_Users9.EOF AND Trim(Request.Form("Management_Region_Name" & x)) <> "" Then
errorArray(errorCount) = Trim(Request.Form("Management_Region_Name" & x))
errorCount = errorCount + 1
End If
else
Response.write x & " - Has been changed <BR><BR>"
Call updateRegion()
End If
End If
End If
Next
Function updateID()
mySQL = "SELECT * FROM CJI_AreaMgrs;"
response.write mysql
Set rs_Users = Con.Execute( mySQL )
IF Not rs_Users.EOF then
sqlupdate = "UPDATE CJI_AreaMgrs SET Regional_Mgr_ID = '" & Trim(Request.Form("Regional_Mgr_ID" & x)) & "' WHERE Area_ID = '" & Trim(Request.Form("Area_ID" & x)) & "';"
Con.Execute( sqlupdate )
End If
End Function
Function updateArea()
mySQL = "SELECT * FROM CJI_AreaMgrs;"
response.write mysql
Set rs_Users = Con.Execute( mySQL )
If Not rs_Users.EOF Then
sqlUpdate = "UPDATE CJI_AreaMgrs SET Manage_Area_Name = '" & Trim(Request.Form("Manage_Area_Name" & x)) & "' WHERE Area_ID = '" & Trim(Request.Form("Area_ID" & x)) & "';"
Con.Execute(sqlUpdate)
End If
End Function
Function updateAreaManager()
mySQL = "SELECT * FROM CJI_AreaMgrs;"
response.write mysql
Set rs_Users = Con.Execute( mySQL )
If Not rs_Users.EOF Then
sqlUpdate = "UPDATE CJI_AreaMgrs SET Area_Mgr = '" & Trim(Request.Form("Area_Mgr" & x)) & "' WHERE Area_ID = '" & Trim(Request.Form("Area_ID" & x)) & "';"
Con.Execute(sqlUpdate)
End If
End Function
Function updateRegionalManager()
mySQL = "SELECT * FROM CJI_RegionMgrs;"
response.write mySQL
Set rs_Users = Con.Execute( mySQL )
If Not rs_Users.EOF Then
sqlUpdate = "UPDATE CJI_RegionMgrs SET Regional_Mgr_Name = '" & Trim(Request.Form("Regional_Mgr_Name" & x)) &"' WHERE Regional_Mgr_ID = '" & Trim(Request.Form("Regional_Mgr_ID" & x)) & "';"
Con.Execute(sqlUpdate)
End If
End Function
Function updateRegion()
mySQL = "SELECT * FROM CJI_RegionMgrs;"
response.write mySQL
Set rs_Users = Con.Execute( mySQL )
If Not rs_Users.EOF Then
sqlUpdate = "UPDATE CJI_RegionMgrs SET Management_Region_Name = '" & Trim(Request.Form("Management_Region_Name" & x)) &"' WHERE Regional_Mgr_ID = '" & Trim(Request.Form("Regional_Mgr_ID" & x)) & "';"
Con.Execute(sqlUpdate)
End If
End Function
Function updateAreaManagerJapanese()
mySQL = "SELECT * FROM CJI_AreaMgrs;"
response.write mysql
Set rs_Users = Con.Execute( mySQL )
If Not rs_Users.EOF Then
sqlUpdate = "UPDATE CJI_AreaMgrs SET Area_MgrJP = '" & Trim(Request.Form("Area_MgrJP" & x)) & "' WHERE Area_ID = '" & Trim(Request.Form("Area_ID" & x)) & "';"
Con.Execute(sqlUpdate)
End If
End Function
Function updateRegionalManagerJapanese()
mySQL = "SELECT * FROM CJI_RegionMgrs;"
response.write mySQL
Set rs_Users = Con.Execute( mySQL )
If Not rs_Users.EOF Then
sqlUpdate = "UPDATE CJI_RegionMgrs SET Regional_Mgr_Name_JP = '" & Trim(Request.Form("Regional_Mgr_Name_JP" & x)) &"' WHERE Regional_Mgr_ID = '" & Trim(Request.Form("Regional_Mgr_ID" & x)) & "';"
Con.Execute(sqlUpdate)
End If
End Function
Session("errorArray") = errorArray
Session("errorCount") = errorCount
Response.Redirect "maslist.asp?table=" & TableName
%>