Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retaining input data from page

Status
Not open for further replies.

apple17

Programmer
Jul 5, 2005
46
0
0
US
I have a web page where the user inputs lots of data, with a 'participant id'. What I want to do when the user inputs the page is

a. check to see if the PARTICIPANT ID already exists
b. if it doesn't, I store a new record
c. If the PARTICIPANT ID is already in the database, I want to redisplay the page with all the input data and tell the user to change the PARTICIPANT ID because it already exists.

My question is how to retain all the input data (there are about 100 fields (checkboxes) on the page) so the user can update the PARTICIPANT ID and re-submit.

THanks!
 
One method would be to check the participant id prior to submitting the page for processing and not allowing it to submit if the ID already exists. You would use AJAX to call a separate ASP page to check the existance of that ID and return a value to your page.

Another approach would be, on your processing ASP page read in every value and store those values into hidden form fields. If the ID turns out to already exist then you use code to submit the form to the original form page and in that page you retrieve those values and set them into the form page fields.


It's hard to think outside the box when I'm trapped in a cubicle.
 
There are a LOT of fields to save in hidden fields. As I've never used AJAX before, is it hard to use? Could I get this written in a couple hours?
 
AJAX is not that difficult, you can probably locate an existing script to modify to your needs.
Essentially it is just a method to execute a different web page and return the result without leaving the page you are on.

Do a bit of searching on AJAX and SQL query and you should find something.

It's hard to think outside the box when I'm trapped in a cubicle.
 
BTW, if you are going to have to read the form data in anyway it is not that much larger a step to store the data into the hidden form fields. And you have to read the form values if you are going to write them to the database.

AJAX provides some great possibilities but remember it does require Javascript be enabled on the client-side. If it is disabled the whole thing is going to fail.

Take a look at this link:
On this site they show a demo of Ajax specifically looking up an ID in a database to see if it is valid.


It's hard to think outside the box when I'm trapped in a cubicle.
 
to 'store them in the hidden form fields', don't i need to create duplicates of all the fields? Isn't that a big deal. Then how do I know to copy them?
 
Remote scripting techniques like AJAX are great for handling form submissions.

Another approach is to have the form 'POST to itself' and retain the user input even on page refresh. This does not require hidden form fields, javascript, or cookies.

The example might be overkill but I wanted it to be simple yet thorough -- was a tough job. :D

Watch For line wrap if you copy/paste.
Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%><%
Option Explicit

dim table1Summary, table1Caption, newCaption
dim text_field, text_area, check_box, radio_buttons, list_menu
dim submit, complete, item

table1Caption="Fields marked with <span class=""important"">*</span> are required."
newCaption=""

submit=false
complete=false

if request.form("B1")="Submit" then submit=true

text_field=trim(request.form("text_field"))
text_area=trim(request.form("text_area"))
check_box=request.form("check_box")
radio_buttons=request.form("radio_buttons")
list_menu=request.form("list_menu")

if submit=true then
	complete=true
	if not len(text_field)>0 then
		complete=false
		newCaption=newCaption&"<span class=""important"">TextField</span>"
	end if
	if not len(check_box)>0 then
		complete=false
		if len(newCaption)>0 then
			newCaption=newCaption&" and <span class=""important"">CheckBox</span>"
		else
			newCaption=newCaption&"<span class=""important"">CheckBox</span>"
		end if
	end if
	if complete=true then
		' Here is where you can put in a routine to insert DB records
		' or send an email message, or whatever it is you want to do
		table1Caption="All Done!"
	else
		table1Caption=""&newCaption
		if instr(newCaption,"and")>0 then
			table1Caption=table1Caption&" are required fields."
		else
			table1Caption=table1Caption&" is a required field."
		end if
	end if
end if
%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form Exmaple</title>
<style type="text/css">
body{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
}
#form_container {
background-color:#999999;
border:solid thin;
border-color:#333333;
margin-left:30%;
width:400px;
}
caption {
padding:3px;
font-weight:bold;
font-size:0.9em;
}
th {
background-color:#CCCCCC;
font-size:0.8em;
text-align: left;
text-indent: 5px;
width: 90px;
}
td {
background-color:#FFFFFF;
font-size:0.75em;
text-align: left;
text-indent: 5px;
}
.important {color: #FF0000;}
</style>
</head>

<body>
	<div id="form_container">
	<table id="table1" width="100%" cellspacing="0" cellpadding="2">
	  <caption>
	  <%=table1Caption%>
	  </caption>
	  <%if complete=false then ' If the form isn't complete show the form.%>
	  <!--
	  	Notice here form action= nothing. This will force the page
		to POST the input data to itself.	  
	   -->
	  <form action="" method="post">
	  <tr>
		<th scope="row"><span class="important">*</span>TextField</th>
		<td><input name="text_field" type="text" value="<%=text_field%>" size="20"></td>
	  </tr>
	  <tr>
		<th scope="row">TextArea</th>
		<td><textarea name="text_area" cols="30" rows="5"><%=text_area%></textarea></td>
	  </tr>
	  <tr>
		<th scope="row"><span class="important">*</span>CheckBox</th>
		<td><input name="check_box" type="checkbox" value="1"<%if instr(check_box,"1")>0 then%><%=" checked"%><%end if%>>
		<input name="check_box" type="checkbox" value="2"<%if instr(check_box,"2")>0 then%><%=" checked"%><%end if%>>
		<input name="check_box" type="checkbox" value="3"<%if instr(check_box,"3")>0 then%><%=" checked"%><%end if%>>
		<input name="check_box" type="checkbox" value="4"<%if instr(check_box,"4")>0 then%><%=" checked"%><%end if%>></td>
	  </tr>
	  <tr>
		<th scope="row">RadioButtons</th>
		<td><input name="radio_buttons" type="radio" value="1"<%if len(radio_buttons)=0 or radio_buttons="1" then%><%=" checked"%><%end if%>>
		<input name="radio_buttons" type="radio" value="2"<%if radio_buttons="2" then%><%=" checked"%><%end if%>>
		<input name="radio_buttons" type="radio" value="3"<%if radio_buttons="3" then%><%=" checked"%><%end if%>>
		<input name="radio_buttons" type="radio" value="foo"<%if radio_buttons="foo" then%><%=" checked"%><%end if%>></td>		
	  </tr>
	  <tr>
		<th scope="row">ListMenu</th>
		<td><select name="list_menu">
		  <option value="0"<%if len(list_menu)=0 or list_menu="0" then%><%=" selected"%><%end if%>></option>
		  <option value="1"<%if list_menu="1" then%><%=" selected"%><%end if%>>Selection 1</option>
		  <option value="2"<%if list_menu="2" then%><%=" selected"%><%end if%>>Selection 2</option>
		  <option value="cow"<%if list_menu="cow" then%><%=" selected"%><%end if%>>Selection 3</option>
		</select></td>
	  </tr>
	  <tr>
		<td colspan="2" style="text-align:center;background-color:#999999;"><input name="B1" type="submit" id="B1" value="Submit"></td>
	  </tr>
	  </form>
	  <%end if%>
	  <!-- Here I loop thru the form elements just to display what is being posted on each submit -->
	  <!-- I only use code like this for testing, so good idea to remove for production -->
	  <%for each item in request.form%>
	  <tr>
		<td colspan="2"><%=item&" = "&request.form(item)%></td>
	  </tr>
	  <%next%>
	</table>
	</div>
</body>
</html>
There are all kinds of ways to maintain page state; this is just an example of one of them. If some of the veterans here see room for improvement feel free to step in anytime.

-a6m1n0

Curiosity only kills cats.
 
Ummm, you don't need any hidden fields or AJAX or anything. it all comes down to how you setup the page.

Step 1) Make your form. Target it at a second page.
Code:
<html>
<head>
<title>My Form!</title>
</head>
<body>
<form method="POST" action="MySecondPage.asp">
<h1>User Signup Page</h1>
Enter Your Username: <input type="text" name="txtUsername"><br/>
Enter Your Password: <input type="password" name="txtPassword"><br/>
Re-Enter Your Password: <input type="password" name="txtPassword2"><br/>
...
etc
...
</form>
</body>
</html>

Step 2) Place your processing code in the top of your second page. Create an If then loop to Redirect the user to a success page if their username wasn't already in use. After this section paste your form and go back and enter values that pull from the Request.Form collection. They will only see this form if their username was already in use or one of your other validation steps didn't work
Code:
<%
Option Explicit

'--- Validate inputs
Dim allInputsValid : allInputsValid = True
Dim usernameInUse : usernameInUse = False
Dim errorMessage

If Len(Trim(Request.Form("txtUsername"))) < 8 Then
   allInputsValid = False
   errorMessage = "Username must be at least 8 characters.<br/>"
End if

If Len(Trim(Request.Form("txtPassword"))) < 8 Or _
   Request.Form("txtPassword") <> Request.Form("txtPassword2") Then
   allInputsValid = False
   errorMessage = errorMessage & "Password must be at least 8 characters and both must match.<br/>"
End If

'--- Check that the username isn't in use once the form validates
If allInputsValid Then
   Dim conn, sql_str, rs_check
   Set conn = Server.CreateObject("ADODB.Connection")
   conn.Open "your connection string"

   sql_str = "SELECT UsernameField FROM UserTable WHERE UsernameField LIKE '" & Replace(Request.Form("txtUsername"),"'","''") & "'"
   Set rs_check = conn.Execute(sql_str)
   If Not rs_check.EOF Then
      usernameInUse = True
   End If
End If

'--- If everything is ok then add them to the database and redirect to the success page
If allInputsValid and Not usernameInUse Then
   sql_str = "INSERT INTO UserTable(UsernameField, PasswordField) VALUES('" & Replace(Request.Form("txtUsername"),"'","''") & "','" & Replace(Request.Form("txtPassword"),"'","''") & "')"
   conn.Execute sql_str

   Response.Redirect "SuccessPage.asp?Username=" & Request.Form("txtUsername")
End If

'Getting to this point means either the username is a duplicate or there was an invalid field
%>
<html>
<head>
<title>My Form!</title>
</head>
<body>
<form method="POST" action="MySecondPage.asp">
<h1>User Signup Page</h1>
<%
If usernameInUse Then 
   Response.Write "<span style=""color:red;"">Sorry, this username is in use already.</span><br/>"
ElseIf Not allInputsValid Then
   Response.Write errorMessage
End If
%>
Enter Your Username: <input type="text" name="txtUsername" value="<%=Request.Form("txtUsername")%>"><br/>
Enter Your Password: <input type="password" name="txtPassword"><br/>
Re-Enter Your Password: <input type="password" name="txtPassword2"><br/>
...
etc
...
</form>
</body>
</html>

And there you have it.

-T

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top