devxlogo

Passing Information From a Form to Code

Passing Information From a Form to Code

Question:
I want to add a simple form to my Web page, but I can’t figure it out. I want a form that lets my users enter text into a textarea box, and an onSubmit function that will send the text to a nearby frame (or iframe) and save it there for others to see it, like a guest book. I just need name and textarea boxes. How can I go about doing this? Thank you in advance.

Answer:
The following code shows how combining Active Server Pages (ASP) with an HTML form can enable you to pass information from a form to code. If building a guestbook, I would save the names to a database. To conserve space, I didn’t implement that piece, but I did make the names available to ASP, from which the names could be passed to database code.

This page allows users to enter names to a guestbook. All of the names are remembered and displayed in a textarea HTML control. The names could actually be displayed anywhere you want them. The secret is the HTML

and the ASP call to Request.Form().

Request.Form(“control_name”) is used with HTML forms. The HTML Submit button normally takes users to a new page. When a user enters information into an HTML control (such as a textbox) and the Submit button clicked, the contents of the textbox can be extracted from within code running in that new page. For example, the following code will extract the value of the txtLastName textbox and save the value into a variable names strLName.

strLName = Request.Form("txtLastName")

The txtLastName is on the previous page, or the page that called the current page. That textbox (or other HTML control) will always be in the previous page when using Request.Form.

After you have retrieved the value of a textbox into a variable, you can do anything with that value. For example, you can add the name to a mailing list, or customize the page so that it includes the user name on the page, or dynamically construct an e-mail message, and send it to the person named.

The Action parameter of the is set to the ASP page to retrieve the value. Request.Form() will extract the name and do something with it. In this case, I am adding data to an array, then saving the array to a Session variable. Session variables allow data to be passed to from page to page. Try the following code by copying it and pasting it into an ASP page.

<%@ Language=VBScript %><%Dim strInputValDim strUserArraystrInputVal = Request.Form("text1")If Len(strInputVal) > 0 then	strUserArray = Session("GuestBookArray")	If IsArray(strUserArray) Then		intNewArraySize = UBOUND(strUserArray) + 1		Redim Preserve strUserArray(intNewArraySize)	Else		Dim tempArray()		intNewArraySize = 0		redim tempArray(intNewArraySize)		strUserArray = tempArray	End If	strUserArray(intNewArraySize) = strInputVal	Session("GuestBookArray") = strUserArray 	For i = 0 to UBound(strUserArray)		strAllUsers = strAllUsers & strUserArray(i) & vbCrLf	NextEnd if%>

Guestbook

Please sign the guestbook:


Browse the Guestbook: