• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1434
  • Last Modified:

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_commonparts.asp?Action=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
0
lshane
Asked:
lshane
  • 4
  • 3
  • 3
2 Solutions
 
kevp75Commented:
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
0
 
lshaneAuthor Commented:
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("cp_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.
0
 
kevp75Commented:
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")%>" />
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
lshaneAuthor Commented:
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/connPMDData2_DSN.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.ServerVariables("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.asp"
  MM_fieldsStr  = "catID|value|cp_5x5|value|cp_12NC|value|cp_Desc|value|cp_Status|value|cp_Replaced|value"
  MM_columnsStr = "cp_catID|none,none,NULL|cp_5x5|',none,''|cp_12NC|',none,''|cp_Desc|',none,''|cp_Status|',none,''|cp_Replaced|',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_fields(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_commonparts.asp?Action=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.ActiveConnection = MM_editConnection
    MM_editCmd.CommandText = MM_editQuery
    MM_editCmd.Execute
    MM_editCmd.ActiveConnection.Close

    If (MM_editRedirectUrl <> "") Then
      Response.Redirect(MM_editRedirectUrl)
    End If
  End If

End If
%>
<%
Dim rsCPCatList
Dim rsCPCatList_numRows

Set rsCPCatList = Server.CreateObject("ADODB.Recordset")
rsCPCatList.ActiveConnection = 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_Files/convert_style_admin.htm" type=text/css
rel=stylesheet><LINK href="../Preflite_Site_Files/custom_styles.css"
type=text/css rel=stylesheet>
<SCRIPT language=javascript
src="../Preflite_Site_Files/common_scripts.js">//</SCRIPT>

<SCRIPT language=javascript
src="../Preflite_Site_Files/convert_lib.htm">//</SCRIPT>

<SCRIPT src="../Preflite_Site_Files/site_data_admin.js"
type=text/javascript>//</SCRIPT>

<SCRIPT type=text/javascript>
<!--


_page.hideLeftNavigation = true;
//-->
</SCRIPT>

<META content="Microsoft FrontPage 4.0" name=GENERATOR>
<link href="../Preflite_Site_Files/my_styles_admin.css" 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>&nbsp;</TD>
    <TD width=20>&nbsp;</TD>
    <TD width="944" align="center"><strong>ADD COMMON PARTS</strong></TD>
    <TD width=20>&nbsp;</TD>
    <TD width=20>&nbsp;</TD>
  </TR>
  <TR id=p-carow2 vAlign=top>
    <TD id=p-column1>&nbsp;      </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("Action") = "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.Fields.Item("catID").Value)%>">
  <%=(rsCPCatList.Fields.Item("cp_catName").Value)%>
  </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
0
 
b0lsc0ttCommented:
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
0
 
kevp75Commented:
you'd put it right in here:
 For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
    MM_fields(MM_i+1) = CStr(Request.Form(MM_fields(MM_i)))
  Next


basically, you would just set the session variables like:
 For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
    MM_fields(MM_i+1) = CStr(Request.Form(MM_fields(MM_i)))
  Next
'start the session vairables:

session("cp_5x5") = request.form("cp_5x5")
'repeat if you need to do this for more.



I do agree with bol, if it is just the one you are worried about, it may be better to pass it as a querystring
0
 
lshaneAuthor Commented:
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
0
 
b0lsc0ttCommented:
Just to make sure I didn't cause a misunderstanding the query string could be used for more than one "variable" but there is a limit on the total length of the URL (and thus the query string).  However in this case go with the Session variables and keep the URL clean. ;)

bol
0
 
lshaneAuthor Commented:
Thanks, all.  I appreciate it - as always!!!

Shane
0
 
b0lsc0ttCommented:
Your welcome!  Thanks for the generous split; I'm glad I could help a bit.  It was a fun question.

bol
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now