Quick and Dirty Contact Forms.

This isn't much of a tutorial. Just an idea to quickly generate an email / contact form
without the need of a database unless you wanted to save the information as well. Then,
you would just subit to the db using the proper section and the form will still work as
normal.

Firstly, Create a new cfm page called form.fields.cfm and include the following:

<cfset fields = "Name,Email,Company,Address,Address2,URL,Comments">

Now, create another page called form.cfm and include the following:

<cfinclude template="form.fields.cfm">

<!--- IF FORM IS SUBMITTED, SEND EMAIL AND SHOW THANK YOU MESSAGE --->

<cfif IsDefined("process_form")>
    <CFMAIL TO="you@youremail.com"
                   FROM="#Email#"
                   SUBJECT="Your Subject"
                   SERVER="mail.yourserver.com"
                   USERNAME="you@youremail.com"
                   PASSWORD="yourpassword">

      Subject: Contact Form Email

      <cfloop collection="#form#" item="i">
            <br>
            <cfoutput>#i# = #evaluate('form.' & i)#</cfoutput>
            <br>     
      </cfloop>
    </CFMAIL>

    <span class="headtext">Thanks for your message!</span>
    <br />
We will get back to you as soon as possible.

    <!--- IF FORM IS SUBMITTED, SEND EMAIL AND SHOW THANK YOU MESSAGE --->

<cfelse>

    <!--- ELSE, GRAB THE FORM FIELDS FROM MYVARS.CFM AND GENERATE CONTACT FORM --->

    <table border=0 width=300>
    
    <tr>
        
    <td colspan=2><font face='verdana' size=2>Contact Us:</font></td>
        </tr>

        <cfoutput>
        <form action="form.cfm" method="post">
    
    <input type="hidden" name="process_form">
        <cfloop list="#fields#" index="i">
        <tr>    
            <td>
<font face="arial" size=1>#i#</font></td>
            <!--- DON'T SHOW TEXT BOX FOR COMMENTS (IS NOT) --->
            <td>
                <cfif i IS NOT "Comments"><input type="text" name="#i#" width="20"></cfif>
                <!--- ONLY SHOW TEXT AREA (IS) --->
                <cfif i IS "Comments"><textarea name="#i#" cols="55" rows="5"></textarea></cfif>
            </td>    
        </tr>

        </cfloop>
        </cfoutput>

    </table>
    <input type="submit" value="Submit">
    </form>

</cfif>

Now, you'll always need to use the Name, Email and Comments fields for the form to work
correctly but you can add or change all of the rest. Just set your email server details
and the form should work correctly.

NOTE: I wrote this a while back and it wasn't meant for a tutorial but because it's fairly
easy to understand, I am only posting it for ideas.

Chris



All ColdFusion Tutorials By Author: Chris Bunting