lshane
asked on
Classic ASP VBScript: How to retain Input form field values when page redirects to itself.
Classic ASP VBScript
MS Access
DWMX
WIN XP PRO
Hello. I am trying to get a form field to retain its entered value if the page is reloaded from an "Error Redirect".
The page is using the DWMX INSERT behavior, so it is posting to itself.
I have a block of code entered before the INSERT script that basically says to redirect back to this page if the "catID" = 0. Here's that block of code:
========================== ========== ========== ========== ======
<%if (CStr(request("catID")) = "0") then%>
<%response.Redirect("add_c ommonparts .asp?Actio n=First")% >
<%end if%>
========================== ========== ========== ========== ======
Problem: I would like another field in that form ("cp_5x5") to retain its value, but it blows it away when the page redirects back to itself. How can I achieve this?
I have read some possibilities regarding Hidden fields, but am not sure how to implement this. I do, however, want to keep validation server-side (ASP).
Thanks so much,
Shane
MS Access
DWMX
WIN XP PRO
Hello. I am trying to get a form field to retain its entered value if the page is reloaded from an "Error Redirect".
The page is using the DWMX INSERT behavior, so it is posting to itself.
I have a block of code entered before the INSERT script that basically says to redirect back to this page if the "catID" = 0. Here's that block of code:
==========================
<%if (CStr(request("catID")) = "0") then%>
<%response.Redirect("add_c
<%end if%>
==========================
Problem: I would like another field in that form ("cp_5x5") to retain its value, but it blows it away when the page redirects back to itself. How can I achieve this?
I have read some possibilities regarding Hidden fields, but am not sure how to implement this. I do, however, want to keep validation server-side (ASP).
Thanks so much,
Shane
you may want to store the values in session variables. Then in the form, set the values of the fields to be their co-responding session variables
ASKER
Hi, kevp75. Thank you. Could you, please, help me out with that. I was trying some of that, but apparently I'm not placing things where they belong.
I tried applying a "value="<%=request.form("c p_5x5")%>" for the value of that field. So, when it redirects back to itself, I hoped to see it remain in the text field; however, no go.
Do I have the syntax correct? I'm certain I'm missing it somewhere.
I tried applying a "value="<%=request.form("c
Do I have the syntax correct? I'm certain I'm missing it somewhere.
can you post out the entire field tag?
you have the basics of it right, what I was suggesting was where you do your processing to add in some session variables, and in the field itself (after processing the form) have the session variable in the field value....
so if in your form processing code you have:
<%
strcp_5x5 = request.form("cp_5x5")
'put this afer it:
session("cp_5x5") = strcp_5x5
%>
then your field would become:
<input type="text" name="cp_5x5" value="<%=session("cp_5x5" )%>" />
you have the basics of it right, what I was suggesting was where you do your processing to add in some session variables, and in the field itself (after processing the form) have the session variable in the field value....
so if in your form processing code you have:
<%
strcp_5x5 = request.form("cp_5x5")
'put this afer it:
session("cp_5x5") = strcp_5x5
%>
then your field would become:
<input type="text" name="cp_5x5" value="<%=session("cp_5x5"
ASKER
Hi, kevp75. OK - it's good to know I'm close. I'll post the majority of the code here (I know DWMX is frowned upon, but please bear with me):
========================== ========== ========== ========== ========== ========== =======
<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="../Connections/connP MDData2_DS N.asp" -->
<%
' *** Edit Operations: declare variables
Dim MM_editAction
Dim MM_abortEdit
Dim MM_editQuery
Dim MM_editCmd
Dim MM_editConnection
Dim MM_editTable
Dim MM_editRedirectUrl
Dim MM_editColumn
Dim MM_recordId
Dim MM_fieldsStr
Dim MM_columnsStr
Dim MM_fields
Dim MM_columns
Dim MM_typeArray
Dim MM_formVal
Dim MM_delim
Dim MM_altVal
Dim MM_emptyVal
Dim MM_i
MM_editAction = CStr(Request.ServerVariabl es("SCRIPT _NAME"))
If (Request.QueryString <> "") Then
MM_editAction = MM_editAction & "?" & Request.QueryString
End If
' boolean to abort record edit
MM_abortEdit = false
' query string to execute
MM_editQuery = ""
%>
<%
' *** Insert Record: set variables
If (CStr(Request("MM_insert") ) = "form1") Then
MM_editConnection = MM_connPMDData2_DSN_STRING
MM_editTable = "common_parts"
MM_editRedirectUrl = "commonparts_list_select.a sp"
MM_fieldsStr = "catID|value|cp_5x5|value| cp_12NC|va lue|cp_Des c|value|cp _Status|va lue|cp_Rep laced|valu e"
MM_columnsStr = "cp_catID|none,none,NULL|c p_5x5|',no ne,''|cp_1 2NC|',none ,''|cp_Des c|',none,' '|cp_Statu s|',none,' '|cp_Repla ced|',none ,''"
' create the MM_fields and MM_columns arrays
MM_fields = Split(MM_fieldsStr, "|")
MM_columns = Split(MM_columnsStr, "|")
' set the form values
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_fields(MM_i+1) = CStr(Request.Form(MM_field s(MM_i)))
Next
' append the query string to the redirect URL
If (MM_editRedirectUrl <> "" And Request.QueryString <> "") Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And Request.QueryString <> "") Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
End If
End If
End If
%>
<%if (CStr(request("catID")) = "0") then%>
<%response.Redirect("add_c ommonparts .asp?Actio n=First")% >
<%end if%>
<%
' *** Insert Record: construct a sql insert statement and execute it
Dim MM_tableValues
Dim MM_dbValues
If (CStr(Request("MM_insert") ) <> "") Then
' create the sql insert statement
MM_tableValues = ""
MM_dbValues = ""
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_formVal = MM_fields(MM_i+1)
MM_typeArray = Split(MM_columns(MM_i+1)," ,")
MM_delim = MM_typeArray(0)
If (MM_delim = "none") Then MM_delim = ""
MM_altVal = MM_typeArray(1)
If (MM_altVal = "none") Then MM_altVal = ""
MM_emptyVal = MM_typeArray(2)
If (MM_emptyVal = "none") Then MM_emptyVal = ""
If (MM_formVal = "") Then
MM_formVal = MM_emptyVal
Else
If (MM_altVal <> "") Then
MM_formVal = MM_altVal
ElseIf (MM_delim = "'") Then ' escape quotes
MM_formVal = "'" & Replace(MM_formVal,"'","'' ") & "'"
Else
MM_formVal = MM_delim + MM_formVal + MM_delim
End If
End If
If (MM_i <> LBound(MM_fields)) Then
MM_tableValues = MM_tableValues & ","
MM_dbValues = MM_dbValues & ","
End If
MM_tableValues = MM_tableValues & MM_columns(MM_i)
MM_dbValues = MM_dbValues & MM_formVal
Next
MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues & ") values (" & MM_dbValues & ")"
If (Not MM_abortEdit) Then
' execute the insert
Set MM_editCmd = Server.CreateObject("ADODB .Command")
MM_editCmd.ActiveConnectio n = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnectio n.Close
If (MM_editRedirectUrl <> "") Then
Response.Redirect(MM_editR edirectUrl )
End If
End If
End If
%>
<%
Dim rsCPCatList
Dim rsCPCatList_numRows
Set rsCPCatList = Server.CreateObject("ADODB .Recordset ")
rsCPCatList.ActiveConnecti on = MM_connPMDData2_DSN_STRING
rsCPCatList.Source = "SELECT * FROM common_parts_cat ORDER BY cp_catName ASC"
rsCPCatList.CursorType = 0
rsCPCatList.CursorLocation = 2
rsCPCatList.LockType = 1
rsCPCatList.Open()
rsCPCatList_numRows = 0
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML xmlns:xhtml = "http://www.w3.org/1999/xhtml"><HEAD><TITLE>Preflite Home</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META content="Your Keywords - Used for search robots" name=keyword><LINK
href="../Preflite_Site_Fil es/convert _style_adm in.htm" type=text/css
rel=stylesheet><LINK href="../Preflite_Site_Fil es/custom_ styles.css "
type=text/css rel=stylesheet>
<SCRIPT language=javascript
src="../Preflite_Site_File s/common_s cripts.js" >//</SCRIP T>
<SCRIPT language=javascript
src="../Preflite_Site_File s/convert_ lib.htm">/ /</SCRIPT>
<SCRIPT src="../Preflite_Site_File s/site_dat a_admin.js "
type=text/javascript>//</S CRIPT>
<SCRIPT type=text/javascript>
<!--
_page.hideLeftNavigation = true;
//-->
</SCRIPT>
<META content="Microsoft FrontPage 4.0" name=GENERATOR>
<link href="../Preflite_Site_Fil es/my_styl es_admin.c ss" rel="stylesheet" type="text/css">
</HEAD>
<BODY>
<BODY>
<SCRIPT type=text/javascript>_page .startPage ()</SCRIPT >
<TABLE width="1024" border=0 cellPadding=0 cellSpacing=0 id=p-ca>
<TBODY>
<TR id=p-carow1>
<TD width=20> </TD>
<TD width=20> </TD>
<TD width="944" align="center"><strong>ADD COMMON PARTS</strong></TD>
<TD width=20> </TD>
<TD width=20> </TD>
</TR>
<TR id=p-carow2 vAlign=top>
<TD id=p-column1> </TD>
<TD></TD>
<TD width=496>
<form ACTION="<%=MM_editAction%> " METHOD="POST" name="form1">
<table width="700" align="center" class="tablebrdrgray">
<tr valign="top">
<td width="350" align="right" nowrap>List Name:</td>
<td width="350"><%if request.QueryString("Actio n") = "First" then%>
<b><font color="#FF0000">You must select a list!</font></b><br>
<%end if%><select name="catID" id="catID">
<option value="0">Select List</option>
<%
While (NOT rsCPCatList.EOF)
%>
<option value="<%=(rsCPCatList.Fie lds.Item(" catID").Va lue)%>">
<%=(rsCPCatList.Fields.Ite m("cp_catN ame").Valu e)%>
</option>
<%
rsCPCatList.MoveNext()
Wend
If (rsCPCatList.CursorType > 0) Then
rsCPCatList.MoveFirst
Else
rsCPCatList.Requery
End If
%>
</select>
</td>
</tr>
<tr valign="top">
<td align="right" nowrap>5x5 Part Number:</td>
<td><input name="cp_5x5" type="text" id="cp_5x5" size="40" value="<%= Session("cp_5x5") %>"></td>
</tr>
<tr valign="top">
<td align="right" nowrap>12NC Part Number:</td>
<td><input type="submit" value="Add Part"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
</BODY>
</HTML>
<%
rsCPCatList.Close()
Set rsCPCatList = Nothing
%>
========================== ========== ========== ========== ========== ========== =======
Thanks so much,
Shane
==========================
<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="../Connections/connP
<%
' *** Edit Operations: declare variables
Dim MM_editAction
Dim MM_abortEdit
Dim MM_editQuery
Dim MM_editCmd
Dim MM_editConnection
Dim MM_editTable
Dim MM_editRedirectUrl
Dim MM_editColumn
Dim MM_recordId
Dim MM_fieldsStr
Dim MM_columnsStr
Dim MM_fields
Dim MM_columns
Dim MM_typeArray
Dim MM_formVal
Dim MM_delim
Dim MM_altVal
Dim MM_emptyVal
Dim MM_i
MM_editAction = CStr(Request.ServerVariabl
If (Request.QueryString <> "") Then
MM_editAction = MM_editAction & "?" & Request.QueryString
End If
' boolean to abort record edit
MM_abortEdit = false
' query string to execute
MM_editQuery = ""
%>
<%
' *** Insert Record: set variables
If (CStr(Request("MM_insert")
MM_editConnection = MM_connPMDData2_DSN_STRING
MM_editTable = "common_parts"
MM_editRedirectUrl = "commonparts_list_select.a
MM_fieldsStr = "catID|value|cp_5x5|value|
MM_columnsStr = "cp_catID|none,none,NULL|c
' create the MM_fields and MM_columns arrays
MM_fields = Split(MM_fieldsStr, "|")
MM_columns = Split(MM_columnsStr, "|")
' set the form values
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_fields(MM_i+1) = CStr(Request.Form(MM_field
Next
' append the query string to the redirect URL
If (MM_editRedirectUrl <> "" And Request.QueryString <> "") Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And Request.QueryString <> "") Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
End If
End If
End If
%>
<%if (CStr(request("catID")) = "0") then%>
<%response.Redirect("add_c
<%end if%>
<%
' *** Insert Record: construct a sql insert statement and execute it
Dim MM_tableValues
Dim MM_dbValues
If (CStr(Request("MM_insert")
' create the sql insert statement
MM_tableValues = ""
MM_dbValues = ""
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_formVal = MM_fields(MM_i+1)
MM_typeArray = Split(MM_columns(MM_i+1),"
MM_delim = MM_typeArray(0)
If (MM_delim = "none") Then MM_delim = ""
MM_altVal = MM_typeArray(1)
If (MM_altVal = "none") Then MM_altVal = ""
MM_emptyVal = MM_typeArray(2)
If (MM_emptyVal = "none") Then MM_emptyVal = ""
If (MM_formVal = "") Then
MM_formVal = MM_emptyVal
Else
If (MM_altVal <> "") Then
MM_formVal = MM_altVal
ElseIf (MM_delim = "'") Then ' escape quotes
MM_formVal = "'" & Replace(MM_formVal,"'","''
Else
MM_formVal = MM_delim + MM_formVal + MM_delim
End If
End If
If (MM_i <> LBound(MM_fields)) Then
MM_tableValues = MM_tableValues & ","
MM_dbValues = MM_dbValues & ","
End If
MM_tableValues = MM_tableValues & MM_columns(MM_i)
MM_dbValues = MM_dbValues & MM_formVal
Next
MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues & ") values (" & MM_dbValues & ")"
If (Not MM_abortEdit) Then
' execute the insert
Set MM_editCmd = Server.CreateObject("ADODB
MM_editCmd.ActiveConnectio
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnectio
If (MM_editRedirectUrl <> "") Then
Response.Redirect(MM_editR
End If
End If
End If
%>
<%
Dim rsCPCatList
Dim rsCPCatList_numRows
Set rsCPCatList = Server.CreateObject("ADODB
rsCPCatList.ActiveConnecti
rsCPCatList.Source = "SELECT * FROM common_parts_cat ORDER BY cp_catName ASC"
rsCPCatList.CursorType = 0
rsCPCatList.CursorLocation
rsCPCatList.LockType = 1
rsCPCatList.Open()
rsCPCatList_numRows = 0
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML xmlns:xhtml = "http://www.w3.org/1999/xhtml"><HEAD><TITLE>Preflite Home</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META content="Your Keywords - Used for search robots" name=keyword><LINK
href="../Preflite_Site_Fil
rel=stylesheet><LINK href="../Preflite_Site_Fil
type=text/css rel=stylesheet>
<SCRIPT language=javascript
src="../Preflite_Site_File
<SCRIPT language=javascript
src="../Preflite_Site_File
<SCRIPT src="../Preflite_Site_File
type=text/javascript>//</S
<SCRIPT type=text/javascript>
<!--
_page.hideLeftNavigation = true;
//-->
</SCRIPT>
<META content="Microsoft FrontPage 4.0" name=GENERATOR>
<link href="../Preflite_Site_Fil
</HEAD>
<BODY>
<BODY>
<SCRIPT type=text/javascript>_page
<TABLE width="1024" border=0 cellPadding=0 cellSpacing=0 id=p-ca>
<TBODY>
<TR id=p-carow1>
<TD width=20> </TD>
<TD width=20> </TD>
<TD width="944" align="center"><strong>ADD
<TD width=20> </TD>
<TD width=20> </TD>
</TR>
<TR id=p-carow2 vAlign=top>
<TD id=p-column1> </TD>
<TD></TD>
<TD width=496>
<form ACTION="<%=MM_editAction%>
<table width="700" align="center" class="tablebrdrgray">
<tr valign="top">
<td width="350" align="right" nowrap>List Name:</td>
<td width="350"><%if request.QueryString("Actio
<b><font color="#FF0000">You must select a list!</font></b><br>
<%end if%><select name="catID" id="catID">
<option value="0">Select List</option>
<%
While (NOT rsCPCatList.EOF)
%>
<option value="<%=(rsCPCatList.Fie
<%=(rsCPCatList.Fields.Ite
</option>
<%
rsCPCatList.MoveNext()
Wend
If (rsCPCatList.CursorType > 0) Then
rsCPCatList.MoveFirst
Else
rsCPCatList.Requery
End If
%>
</select>
</td>
</tr>
<tr valign="top">
<td align="right" nowrap>5x5 Part Number:</td>
<td><input name="cp_5x5" type="text" id="cp_5x5" size="40" value="<%= Session("cp_5x5") %>"></td>
</tr>
<tr valign="top">
<td align="right" nowrap>12NC Part Number:</td>
<td><input type="submit" value="Add Part"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
</BODY>
</HTML>
<%
rsCPCatList.Close()
Set rsCPCatList = Nothing
%>
==========================
Thanks so much,
Shane
It looks like you are moving towards using a Session variable for this. If there is only one item and you are already doing a redirect I will often just use the query string for it. Session variables are great and there are definitely times where I need and use them. However in this case it seems you don't have to. The downside of a Session variable is it does take a cookie to keep track of it and in some cases they could be disabled or refused by the browser/user. Session variables wouldn't work in that case.
Let me know if you want details on doing this in the query string. The basic idea is the same as what you are doing for a Session variable and it is usually easy to do.
bol
Let me know if you want details on doing this in the query string. The basic idea is the same as what you are doing for a Session variable and it is usually easy to do.
bol
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks, bol and kevp75. Well, I actually do have 5 fields total (1 contains 2 radio buttons - the other 4 are TEXT fields).
This will be for an intranet, so everyone should have their cookies "On".
I'll give your suggestion a shot, kevp75.
Thanks so much,
Shane
This will be for an intranet, so everyone should have their cookies "On".
I'll give your suggestion a shot, kevp75.
Thanks so much,
Shane
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks, all. I appreciate it - as always!!!
Shane
Shane
Your welcome! Thanks for the generous split; I'm glad I could help a bit. It was a fun question.
bol
bol