The example below provides basic query string based form validation
Simple form:
<form name="form1" method="post" action="form.asp?pa=1">
Full Name: <input type="text" name="txt_full_name">
Location: <input type="text" name="txt_location">
<input type="submit" value="Submit Form">
</form>
Retrieving the form variables:
<%
'Form variables for form1
form_full_name=Request.Form("txt_full_name")
form_location=Request.Form("txt_location")
%>
Query string to include in form.asp:
<%
'Error Message Query String
ErrMsg=Request.QueryString("em")
'Page Function identifier
PageAction=Request.QueryString("pa")
%>
Add form error messages:
<%
'Example error Messages
If ErrMsg = "bfn" then
ErrorMessage="Blank full name detected"
Elseif ErrMsg = "bl" then
ErrorMessage="Blank location detected"
Elseif ErrMsg = "mfn" then
ErrorMessage="Maximum Characters exceeded in Full Name"
Elseif ErrMsg = "ml" then
ErrorMessage="Maximum Characters exceeded in location"
Elseif ErrMsg = "ufn" then
ErrorMessage="Unidentified Characters in Full Name"
Elseif ErrMsg = "ul" then
ErrorMessage="Unidentified Characters in location"
End If
%>
Add Page function if receiving the form data in same page - prevents function loop
<%
'Select form variables and check values if form is submitted
If PageAction = "1" then
%>
Form error handling:
<%
'character length check - 0 value
If Len(form_full_name)= 0 then Response.Redirect("form.asp?em=bfn") End If
If Len(form_location)= 0 then Response.Redirect("form.asp?em=bl") End If
'character length maximum of 15 characters
If Len(form_full_name)> 15 then Response.Redirect("form.asp?em=mfn") End If
If Len(form_location)> 15 then Response.Redirect("form.asp?em=ml") End If
'Character check in form values
If Instr(form_full_name,"%") > 0 then Response.Redirect("form.asp?em=ufn") End If 'Checks if a percentage character was inputted
If Instr(form_location,"+") > 0 then Response.Redirect("form.asp?em=ul") End If 'Checks if a plus character was inputted
%>
<%
'End If PageAction = "1" then
End If
%>
Code all together: form.asp
<%
'Error Message Query String
ErrMsg=Request.QueryString("em")
'Page Function identifier
PageAction=Request.QueryString("pa")
%>
<%
'Example error Messages
If ErrMsg = "bfn" then
ErrorMessage="Blank full name detected"
Elseif ErrMsg = "bl" then
ErrorMessage="Blank location detected"
Elseif ErrMsg = "mfn" then
ErrorMessage="Maximum Characters exceeded in Full Name"
Elseif ErrMsg = "ml" then
ErrorMessage="Maximum Characters exceeded in location"
Elseif ErrMsg = "ufn" then
ErrorMessage="Unidentified Characters in Full Name"
Elseif ErrMsg = "ul" then
ErrorMessage="Unidentified Characters in location"
End If
%>
<html>
<body>
<%
'Display Error Message
Response.Write(ErrorMessage)
%>
<br>
<form name="form1" method="post" action="form.asp?pa=1">
Full Name: <input type="text" name="txt_full_name">
Location: <input type="text" name="txt_location">
<input type="submit" value="Submit Form">
</form>
</body>
</html>
<%
'Select form variables and check values if form is submitted
If PageAction = "1" then
%>
<%
'Form variables for form1
form_full_name=Request.Form("txt_full_name")
form_location=Request.Form("txt_location")
%>
<%
'character length check - 0 value
If Len(form_full_name)= 0 then Response.Redirect("form.asp?em=bfn") End If
If Len(form_location)= 0 then Response.Redirect("form.asp?em=bl") End If
'character length maximum of 15 characters
If Len(form_full_name)> 15 then Response.Redirect("form.asp?em=mfn") End If
If Len(form_location)> 15 then Response.Redirect("form.asp?em=ml") End If
'Character check in form values
If Instr(form_full_name,"%") > 0 then Response.Redirect("form.asp?em=ufn") End If 'Checks if a percentage character was inputted
If Instr(form_location,"+") > 0 then Response.Redirect("form.asp?em=ul") End If 'Checks if a plus character was inputted
%>
<%
'End If PageAction = "1" then
End If
%>
<%
'Display form values
Response.write(form_full_name)
Response.write(form_location)
%>
Showing posts with label classic asp. Show all posts
Showing posts with label classic asp. Show all posts
To start the examples, below is a simple method for parsing form variables in asp.
Firstly the simple form:
<html>
<body>
<form name="form1" method="post" action="form.asp">
Full Name: <input type="text" name="txt_full_name">
Location: <input type="text" name="txt_location">
<input type="submit" value="Submit Form">
</form>
</body>
</html>
Secondly is the asp for receiving the form variables:
<%
'Form variables for form1
form_full_name=Request.Form("txt_full_name")
form_location=Request.Form("txt_location")
%>
Lastly to display the retrieved form values:
<%
'Display form values
Response.write(form_full_name)
Response.write(form_location)
%>
You can also write the same form value with out defining a name as shown below:
<%
Response.write(Request.Form("txt_full_name"))
Response.write(Request.Form("txt_location"))
%>
Code all together: form.asp
<html>
<body>
<form name="form1" method="post" action="form.asp">
Full Name: <input type="text" name="txt_full_name">
Location: <input type="text" name="txt_location">
<input type="submit" value="Submit Form">
</form>
</body>
</html>
<%
'Form variables for form1
form_full_name=Request.Form("txt_full_name")
form_location=Request.Form("txt_location")
%>
<%
'Display form values
Response.write(form_full_name)
Response.write(form_location)
%>
Firstly the simple form:
<html>
<body>
<form name="form1" method="post" action="form.asp">
Full Name: <input type="text" name="txt_full_name">
Location: <input type="text" name="txt_location">
<input type="submit" value="Submit Form">
</form>
</body>
</html>
Secondly is the asp for receiving the form variables:
<%
'Form variables for form1
form_full_name=Request.Form("txt_full_name")
form_location=Request.Form("txt_location")
%>
Lastly to display the retrieved form values:
<%
'Display form values
Response.write(form_full_name)
Response.write(form_location)
%>
You can also write the same form value with out defining a name as shown below:
<%
Response.write(Request.Form("txt_full_name"))
Response.write(Request.Form("txt_location"))
%>
Code all together: form.asp
<html>
<body>
<form name="form1" method="post" action="form.asp">
Full Name: <input type="text" name="txt_full_name">
Location: <input type="text" name="txt_location">
<input type="submit" value="Submit Form">
</form>
</body>
</html>
<%
'Form variables for form1
form_full_name=Request.Form("txt_full_name")
form_location=Request.Form("txt_location")
%>
<%
'Display form values
Response.write(form_full_name)
Response.write(form_location)
%>
Posted by
Piers Storey
at
15:07
Labels:
asp,
classic asp,
examples,
form,
parse form values,
Request.Form,
Response.write
After spending the last few years learning asp and making all the mistakes possible I thought I'd add some of the examples I have worked with for those of us who don't take to development quite as quickly as some!
The examples won't be to Einstein standards but do all work and any comments or improvements are welcome. This blog is not for the technically gifted as the majority of the examples will be at best simplistic!
Over the next few weeks, months ect, I will try and cover asp form validation, adding form data to access and sql, automotive emails, classic asp fpdf, simple arrays, searching string values, query stings and other subjects that had me scratching my head when learning.
I hope the examples will be useful and happy coding!
The examples won't be to Einstein standards but do all work and any comments or improvements are welcome. This blog is not for the technically gifted as the majority of the examples will be at best simplistic!
Over the next few weeks, months ect, I will try and cover asp form validation, adding form data to access and sql, automotive emails, classic asp fpdf, simple arrays, searching string values, query stings and other subjects that had me scratching my head when learning.
I hope the examples will be useful and happy coding!
Subscribe to:
Posts (Atom)