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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form submitting to self and not redirecting - unsure where to do redir

Status
Not open for further replies.

leeolive

Programmer
May 14, 2002
46
GB
Hi

I got this code from

I have stripped it down to the basics for example purposes.
At the moment, if user completes all required textboxes, it submits to itself. I need to set it so that if the form is completed properly, it sends all the values to validate page where I will set up my cnnection etc and submit to a db. Where am I going wrong and how can I get the form (after doing checks) to submit to another page?

Hm, not sure also what the DebugQS ia all about. Do I need to keep this in my code?

Much appreciated!
Lee

<%@ Language=VBScript %>
<% Response.Buffer = true %>

<%

Dim bError
Dim bIsSubmitted

bError = false

' IsSubmitted is hidden field in form
If Request.QueryString(&quot;IsSubmitted&quot;) = &quot;&quot; Then
bIsSubmitted = false
Else
bIsSubmitted = true
End If
'Response.Write &quot;bIsSubmitted: &quot; & bIsSubmitted & &quot;<br>&quot;

Function EmailValidation(sFieldname, error)
If bIsSubmitted Then
Dim objRegExp
Set objRegExp = New regexp
objRegExp.Pattern = &quot;^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$&quot;
If Not objRegExp.Test(Request.QueryString(sFieldname)) Then
EmailValidation = &quot; <font color=&quot;&quot;red&quot;&quot;>&quot;&quot;&quot; & error & &quot;&quot;&quot;</font>&quot;
bError = true
Else
EmailValidation = &quot;&quot;

End If
End If
End Function


Function RequiredFieldValidation(sFieldname, error)
RequiredFieldValidation = &quot;&quot;
If bIsSubmitted Then
If Trim(Request.QueryString(sFieldname)) = &quot;&quot; Then
RequiredFieldValidation = &quot; <font color=&quot;&quot;red&quot;&quot;>&quot;&quot;&quot; & error & &quot;&quot;&quot;</font>&quot;
bError = true
End If
End If
End Function

Sub DebugQS()
Response.Write &quot;<b>ScriptName: &quot; & ScriptName() & &quot;</b><br>&quot;
Response.Write &quot;<b>QueryString Variables: </b><br>&quot;
For i=1 to request.QueryString.count
response.write &quot;&nbsp;&nbsp;&quot; & request.QueryString.key(i) & &quot; = &quot;&quot;&quot; & request.QueryString.item(i) & &quot;&quot;&quot;<br>&quot;
If InStr(request.QueryString.item(i), &quot;,&quot;) > 0 Then
For Each item in request.QueryString.item(i)
Response.Write &quot;&nbsp;&nbsp;&nbsp;&nbsp;&quot; & request.QueryString.key(i) & &quot; = &quot;&quot;&quot; & item & &quot;&quot;&quot;<BR>&quot;
Next
End If
Next
End Sub

Function ScriptName()
ScriptName = Right(Request.ServerVariables(&quot;SCRIPT_NAME&quot;), Len(Request.ServerVariables(&quot;SCRIPT_NAME&quot;)) - InStrRev(Request.ServerVariables(&quot;SCRIPT_NAME&quot;), &quot;/&quot;))
End Function

Sub DisplayForm()
%>

<form method=&quot;GET&quot;>
<input type=&quot;hidden&quot; name=&quot;IsSubmitted&quot; value=&quot;Yes&quot;>

<table class=&quot;insettabletop&quot; width=&quot;480&quot; align=&quot;center&quot; border=&quot;0&quot; cellspacing=&quot;4&quot; cellpadding=&quot;4&quot;>
<tr>
<td align=&quot;left&quot;>First Name(s) in full</td>
<td align=&quot;left&quot; width=&quot;5&quot;> &nbsp; </td>
<td align=&quot;left&quot;><input type=&quot;text&quot; name=&quot;FirstName&quot; class=&quot;inputpension&quot; value=&quot;<%=Request.QueryString(&quot;FirstName&quot;)%>&quot; /><img src=&quot;images/required_image.gif&quot; width=&quot;10&quot; height=&quot;9&quot; alt=&quot;Expat Financial&quot; />
<%=RequiredFieldValidation(&quot;FirstName&quot;, &quot;*&quot;)%>
</td>
</tr>
</table>

</form>
<%
DebugQS
End Sub
%>

<HTML>
<HEAD>
<META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;>
<title>Form Validation Test</title>
</HEAD>

<BODY>
<h3>Form Validation Test</h3>
<%
If Not bIsSubmitted Then
DisplayForm
Else
DisplayForm
Response.Write &quot;<br><a href=&quot;&quot;javascript:void(location.replace('formtest.asp'));&quot;&quot;><font color=&quot;&quot;blue&quot;&quot;>Start Again</font></a>&quot;

If Not bError Then
' Clear headers and start over
Response.Clear
%>
<HTML>
<HEAD>
<META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;>
<title>Form Validation Test</title>
</HEAD>
<BODY>
<h3>Form Validation Test</h3>
<%
DebugQS

' Update the DB
Response.Write &quot;Updating the DB...<br>&quot;
' Report errors (or crash)

' Close or whatever button(link)
Response.Write &quot;<br><a href=&quot;&quot;javascript:void(location.replace('formtest.asp'));&quot;&quot;><font color=&quot;&quot;blue&quot;&quot;>Start Again</font></a>&quot;
End If
End If
%>

</BODY>
</HTML>
 
<form action=&quot;ValidationScript.asp&quot;>

also, use method=&quot;POST&quot; unless you want to have the value pairs show up in the address.

if you use &quot;POST&quot; you can get the form variables from the response collection.
 
Oops, the Forms collection, not the response collection.

i.e. variable = request(&quot;form field&quot;)
 
Thanks for getting back!
If i put the form action in then it just redirects to that page immediately without performing the validation. How do I get it to do the validation first and then if it is succesful redirect to that page?

Also, I really don't understand what is happening with this part of the code below. Somewhere here it is redirecting to itself and printing out all the values, which is fine for testing but I don't want that to happen now. I can't figure out whether it's happening in the DebugQS part or where...


<BODY>
<h3>Form Validation Test</h3>
<%
If Not bIsSubmitted Then
DisplayForm
Else
DisplayForm
Response.Write &quot;<br><a href=&quot;&quot;javascript:void(location.replace('formtest.asp'));&quot;&quot;><font color=&quot;&quot;blue&quot;&quot;>Start Again</font></a>&quot;

If Not bError Then
' Clear headers and start over
Response.Clear
%>
<HTML>
<HEAD>
<META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;>
<title>Form Validation Test</title>
</HEAD>
<BODY>
<h3>Form Validation Test</h3>
<%
DebugQS

' Update the DB
Response.Write &quot;Updating the DB...<br>&quot;
' Report errors (or crash)

' Close or whatever button(link)
Response.Write &quot;<br><a href=&quot;&quot;javascript:void(location.replace('formtest.asp'));&quot;&quot;><font color=&quot;&quot;blue&quot;&quot;>Start Again</font></a>&quot;
End If
End If
%>

</BODY>
</HTML>
 
DebugQS is taking the query string and dumping out a list of all the key/value pairs. You don't need it unless you are having trouble with expected values, and you need to see what they're doing. This script is designed to use QueryStrings, not the forms collection, so my advice to use &quot;POST&quot; won't work unless you change lines like this:

Request.QueryString(&quot;IsSubmitted&quot;)

to this:

Request(&quot;IsSubmitted&quot;)

There are a couple of ways to do what you want. I prefer to use javascript for validation, and use javascript to submit the form based upon success or failure of the javascript validation function. The advantage of using javascript to validate and submit is that if the validation fails, the page doesn't go anywhere. You can set focus on the first failing field, etc...

the line in javascript that does the form submit (after validation) looks like this:

window.YourForm.submit();

There's no need to send this to another script if you don't want to. If you do want it to post to a new script, add the &quot;action=&quot; line shown in earlier post to do this.

Alternatively, you can leave out action, and the script will send the form back to itself. Then you need the following line to launch a new page:

Response.Redirect(&quot;/webroot/Database script.asp?Extras&quot;)

where extras are ALL of the form values you have collected in the following format:

formvariable1=Value1&formvariable2=Value2, etc...

This can get complex, and will run into the limitations on length of query string, which is why using POST form variables is much more desirable than using Request.QueryString to handle moving data around. Additionally, all fields are visible to the use when using QueryStrings, including hidden fields, passwords, etc...

QueryStrings are fine for setting non-vital or hackable pagestates, but not suited for sensitive data.

If you don't use javascript to validate the form, or the same script to do validation and db access, I don't know of a way to send the data to antoher script without using the QueryString method, except for using Session variables to store the data. You would write and refer to these in the following way:

Write to:
Session(&quot;SessionValue1&quot;) = FormVariable1

Read:
NewScriptVariable1 = Session(&quot;SessionValue1&quot;)

Sessions use cookies, with their associated problems (Cookies turned off, partial blocking, etc...) so this is also not generally condiered an advisable method of moving data around.

You're best off using either the javascript validation method, or the same vbscript validation and datawriting method.





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top