Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dynamically creating and submitting multiple forms on one page using ASP

0.00/5 (No votes)
17 Sep 2002 1  
How to test for stage of processing using ASP, and then dynamically build your ASP page around this.

Introduction

This is a very cool technique, which saves me a lot of hassle, and I hope it will make all your lives easier too. However, I cannot take the credit for the original idea - it was my boss's brainchild. Fortunately he likes to share knowledge, and so this article is born.

The problem

Now as well all know, for user input to be processed, the form must be submitted to a page which can process it. What if we didn't want to have whole strings of files in each directory, all trying to process one transaction? Below is a way this can be accomplished with only one page.

The solution

The first principle to be understood here is the value of hidden text boxes in field submission. We also create 2 ASP variables - Action and FormAction. A hidden text box will always hold the value of the FormAction variable thus:

<input type=hidden id=txtHFormAction 
   name=txtHFormAction value='<%=FormAction%>'>

At the top of the page do the following:

<%
Dim Action, FormAction
Action = Request.Form("txtHFormAction")

Note: Action takes the old FormAction value on second entry to the page - if it took the Action value, processing would not be accomplished as it would enter the wrong option of the Select Case below. The fact that it holds nothing on first entry is correct, not a problem :)

The following Select Case determines which part of the page should be processed according to the value held in Action.

Select Case Action

    Case ""
        'First entry to page - set the FormAction
        FormAction = "Submitted"

    Case "Submitted"
        'Get values submitted from form
        Name = Request.Form("txtName")
        Email = Request.Form("txtEmail")

        'Call function to deal with whatever
        'processing you want done Process

End Select

Function Process()
    'Whatever processing, emailing, etc
End Function

%>
<html>
<head><title>Action Test Page</title></head>
<body>

<% 'Action can also determine what gets 

'displayed in the HTML section of your page  %>


<% If Action = "" Then %>
<% 'If this is the first entry to the page and Action 

'is equal to nothing, then display the form to be completed %>


<form action = "" method=post id=frmAction name=frmAction>
<%'note that this form submits to the current page, you can also 

'put the current page's name here instead of empty quotes  %>

Please enter your details below:
<table>
<tr>
<td>Name</td>
<td><input type= text name=txtName id=txtName></td>
</tr>
<tr>
<td>Email</td>
<td><input type= text name=txtEmail id=txtEmail></td>
</tr>
</table>
</form>

<%ElseIf Action = "Submitted then"%>
<%'If this is the second entry to the page display some kind 

'of user friendly message to inform the user that his request has 

'been successfully processed %>

<center> Thankyou for contacting us <% response.write Name%>, 
we shall be in touch with you within 2 working days
</center>

<%End If%> 
</body>
</html> 

Note, you are by no means limited to the number of times you can use Action in a single page. This can be used to break down very long forms into logical pieces so that the user does not feel overwhelmed - e.g.. Submit personal details, then a new form for residential details, once that is submitted display a new form for work details, etc.

This is especially useful for database processing. You can store all these values in hidden text boxes as you go along, and then pull them out at the end, for an insert or update.

A total listing of the code is below, without comments interrupting it. I hope everyone here finds it as useful as I have. Once you have used the method a couple of times, I'm sure you will all find even more new and innovative ways to use this - please let me know when you do! Please note that there are no files included to be downloaded with this tutorial as the functions don't actually do anything, and you can copy & paste anything you want from here. I am trying to share an idea, not code to perform a particular function.

<%
Dim Action, FormAction
Action = Request.Form("txtHFormAction")
Select Case Action
    Case ""
        FormAction = "Submitted"
    Case "Submitted"
        'Get values submitted from form

        Name = Request.Form("txtName")
        Email = Request.Form("txtEmail")
        Process
End Select
Function Process()
    'Whatever processing, emailing, etc

End Function
%>
<html>
<head><title>Action Test Page</title></head>
<body>
<% If Action = "" Then %>
<form action = "" method=post id=frmAction name=frmAction>
Please enter your details below: <table border=0>
<tr>
<td>Name</td>
<td><input type= text name=txtName id=txtName></td>
</tr>

<tr>
<td>Email</td>
<td><input type= text name=txtEmail id=txtEmail></td>
</tr>
</table>
</form>

<%ElseIf Action = "Submitted then"%> <center>
Thankyou for contacting us <% response.write Name %>, 
we shall be in touch with you within 2 working days
</center>
<%End If%>

</body>
</html>

Note that this testing for state of processing can also be used to dynamically decide what should display on your page. I'm currently working on an article to demonstrate how you can test for values coming back in recordsets and use the above method to choose what to display on your page according to the recordset content. Any suggestions would be greatly appreciated as I make no claims to be an author :). Watch this space...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here