When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Thursday, July 15, 1999

Mailing Form Results, Part 2

  • Read Mailing Form Results, Part 1
  • - continued -

    In the previous article I showed you how to create the HTML FORM to use with the automatic form mailing code I am about to reveal. You must take care when creating your HTML FORMs. First, you need to make sure to include the JavaScript function, and prefix your form element names. Once you've created a properly formatted form, though, all you need to do is have the ACTION in the FORM set to this ASP file (code available in text format at the end of this article):


    <%@ Language=VBScript %>
    <% Option Explicit %>
        'The header/footer for the email.
        Const strHeader = "Here are the results of the form:"
        Const strFooter = "Form mailer created by 4GuysFromRolla.com, 1999"
        'Who does this go to?  MAKE SURE TO CHANGE THIS TO YOUR EMAIL ADDRESS!
        Const strTo = "your@emailaddress.com"
        'This information is optional
        Dim strFrom, strSubject, strRedirectURL, strFromPath
        strFrom = Request.Form("txtSendToEmailAddress")
        if Len(strFrom) = 0 then strFrom = strTo
        strSubject = Request.Form("txtEmailSubject")
        if Len(strSubject) = 0 then strSubject = "FORM EMAILER: 4GuysFromRolla.com"
        strRedirectURL = Request.Form("urlSendTo")
        if Len(strRedirectURL) = 0 then strRedirectURL = "/"
        strFromPath = Request.Form("urlFromPath")
        if Len(strFromPath) = 0 then strFromPath = "UNKNOWN"
        Dim strBody
        strBody = strHeader & ( vbCrLf & vbCrLf )
        strBody = strBody & ( "FORM: " & strFromPath & vbCrLf ) & _
              ( "FORM submitted at " & Now() & vbCrLf & vbCrLf )
        dim ix, formElementName, formElementValue, prefix, fldName
        For ix = 1 to Request.Form.Count
            formElementName = Request.Form.Key(ix)
            formElementValue = Request.Form.Item(ix) 
            ' what type of field was that on the form?
            prefix = Left(formElementName,3)
            ' and throw away prefix to get actual field name
            fldName = Mid(formElementName,4)
            ' but change periods to spaces for readability
            fldName = Replace(fldName, "."," ")
            Select Case prefix
                ' if the prefix indicates this is a form field of interest...
                Case "txt","sel","rad","cbo","lst","chk":
                    ' if user didn't answer this question, say so...
                    if Len(formElementValue) = 0 then formElementValue = "UNANSWERED"
                    ' then tack on the name of the field and the answer
                    strBody = strBody & (fldName & ": " & formElementValue & vbCrLf)
            End Select
        strBody = strBody & ( vbCrLf & strFooter )
        'Time to send the email
        Dim objCDO
        Set objCDO = Server.CreateObject("CDONTS.NewMail")
        objCDO.To = strTo
        objCDO.From = strFrom
        objCDO.Subject = strSubject
        objCDO.Body = strBody
        Set objCDO = Nothing
        'Send them to the page specified
        Response.Redirect strRedirectURL
    [View a live demo!]

    Special thanks to Bill Wilkinson for an updated version of the above ASP page...

    This script performs a very simple task: iterate through the form variables, creating a body for an email. Once done, send the email to the administrator via CDO, then send the user onto the page specified by the FORM. If you're unfamiliar with sending mail using CDO, be sure to read Sending Emails in ASP using CDO.

    Here is an example of the email received using the survey form discussed in the previous article:

    Here are the results of the form:
    FORM: /mailform/default.htm
    FORM submitted at 7/15/99 12:31:04 AM
    User Name: Scott Mitchell
    User Age: 18 - 24
    Sex: Male
    I Like Cheese: Yes
    Form mailer created by 4GuysFromRolla.com, 1999

    Hope this has been helpful! It's a nice, ultra-reusable script, so go wild with it and use it everywhere!! :)

  • Read Mailing Form Results, Part 1
  • Attachments:

  • The HTML FORM code in text format
  • mailForm.asp in text format

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article