Site Banner

Simple Online Form in ASP


This page introduces a simple program in ASP to create an online form and send the form results by email.

Sample ASP Online Form Program

<form method="POST" action="ASPformEmailResults.asp">

<input type="hidden" name="To" value="abcde@csudh.edu"/>
<input type="hidden" name="Subject" value="online feedback"/>

<label for="from">Your email address: </label><input type="text" name="From"/> <br />
<label for="major">Major: </label><input type="text" name="major"/> <br />
<label for="feedback">Feedback: </label><textarea name="feedback" rows="5" cols="20" wrap="physical" > </textarea>
<input type="submit" />

</form>

ASP sample online form program explanation:

The above program displays an online form with two hidden variables, two text input boxes, one text area input box, and a submit button. The two hidden variables are "To" and "Subject". In most cases, you do not need to ask the user where to send the form results and what is the subject line on the email message of the form results. That is why they were put as hidden variables. You can have more than two hidden variables. There are two text input boxes with labels "Your email address" and "Major" followed by a text area input box with label "Feedback". A submit button is at the end of the form.

  • Use ASPformEmailResults.asp program with Post method to process the online form results. ASPformEmailResults.asp is the file name of the form result processing program.

    <form method="POST" action="ASPformEmailResults.asp">

  • There are two hidden fields, "To" and "Subject". The value of the "To" field is the email address which the form results will be sent.

    <input type="hidden" name="To" value="abcde@csudh.edu"/>

  • The value of "Subject" is the subject line in the email message.

    <input type="hidden" name="Subject" value="online feedback"/>

  • There are three form fields which require the user to fill out. They are "From", "major", and "feedback". The label of the "from" form field is "Your email address". "From" is a field name variable, and "Your email address" is a label displays on the browser. The label does not have to match the field name as long as you know what it is.

    <label for="from">Your email address: </label><input type="text" name="From"/> <br />
    <label for="major">Major: </label><input type="text" name="major"/> <br />
    <label for="feedback">Feedback: </label><textarea name="feedback" rows="5" cols="20" wrap="physical" > </textarea>

Form result process program

Below is the ASP program, ASPformEmailResults.asp, which process the form result.
The file name of this program has to match the value in the action attribute in the online form program.

<form method="POST" action="ASPformEmailResults.asp">

Sample Form Result Processing Program

<%
Dim mail, body

body = "Major: " & Request.Form("major") & vbcrlf & vbcrlf & "Feedback: " & Request.Form("feedback")

Set mail = Server.CreateObject("CDO.Message")
mail.To = Request.Form("To")
mail.From = Request.Form("From")
mail.Subject = Request.Form("Subject")
mail.TextBody = body
mail.Send()

Response.Write("Thanks for submitting the feedback.")

Set mail = nothing
Set body = nothing
%>

ASP sample form result processing program explanation:

The above online form result processing program sends the form results by email.

  • The DIM statement creates variable "mail" and variable "body".

    Dim mail, body

  • The variable "Body" contains the value of the email message content. Use the "&" sign to connect two variables together in a string. Vbcrlf adds a line break. Use "& vbcrlf & vbcrlf" to create a blank line.

    body = "Major: " & Request.Form("major") & vbcrlf & vbcrlf & "Feedback: " & Request.Form("feedback")

    In this case, the message content will be:
    First line: Major:, followed by the value in the form field "major".
    Second line: a blank line.
    Third line: Feedback: , followed by the value in the form field "feedback".
  • "Major: " and "Feedback: " are the display field names (labels) appear in the email message.
  • Request.Form("major") - calls the value of the online-form field "major".
  • The section below set up the email message with the values from the online form and sends the message.

    Set mail = Server.CreateObject("CDO.Message")
    mail.To = Request.Form("To")
    mail.From = Request.Form("From")
    mail.Subject = Request.Form("Subject")
    mail.TextBody = body
    mail.Send()

  • Send the message to the email address mentioned in the form field "To".

    mail.To = Request.Form("To")

  • Use the value of the form field "From" as the sent from address.

    mail.From = Request.Form("From")

  • Use the value of the form field "Subject" as the message subject.

    mail.Subject = Request.Form("Subject")

  • Use the value of the "body" variable set up at the beginning of this program as the message content which includes all the rest of the form fields in it.

    mail.TextBody = Body

  • The above example has only two online form fields to add in the message body area. If you have more than two form fields, add them at the end of the body string.
    For example, if you have another form field named "Student_ID", then the "body" string will be:

    body = "Major: " & Request.Form("major") & vbcrlf & vbcrlf & "Feedback: " & Request.Form("feedback") & vbcrlf & "Student ID: "& Request.Form("Student_ID")

  • The statement below displays the message, "Thanks for submitting the feedback.", on a new browser page after the message was sent.

    Response.Write("Thanks for submitting the feedback.")

  • At the end, destroy the mail and body objects.

    Set mail = nothing
    Set body = nothing

Sample Form Results Sent by E-mail

Below is a sample email message sent from the online form with the Student_ID field added.

From: aaa@bbb.ccc
To: abcde@csudh.edu
Subject: online feedback

Major: History

Feedback:
I love history!
Student ID: 010101010