Skip to main content
Inspiring
April 11, 2006
Question

<cfif form.secret_word EQ #session.secretWord#> thank you <cfelse>sorry </cfif>

  • April 11, 2006
  • 2 replies
  • 658 views
If users enter the secret_word which coincides with the session.secretWord then the form can continue and do its validating. If not the form stops and the cfelse message is shown. The cfif code is here:
<cfif form.secret_word EQ #session.secretWord#>
Thank you for your message.
<cfelse>
Sorry, the secret word you entered in the field does not match the word in the image
Please hit your back button</cfif>


My actionpage is here and I would be very greatful if someone could show me where the cfif should go. Thanks:

<cfinclude template="topnav.cfm">
<!--- Must create this default structure or we pull an error --->
<cfparam name="session.contact_nfo" default="">
<cfif NOT IsStruct(session.contact_nfo)>
<cfset session.contact_nfo = StructNew()>
</cfif>

<!-- Now we create default variable using cfparam -->
<cfparam name="Form.terms" default="NO">
<cfparam name="session.contact_nfo.Name" default="">
<cfparam name="Form.Country" default="">
<cfparam name="Form.City" default="">
<cfparam name="Form.Telephone" default="">
<cfparam name="session.contact_nfo.email" default="">
<cfparam name="session.contact_nfo.comments" default="">

<!-- Now we clear the Session of any Values to allow us to place our new form Values passed from our form page -->
<cfset StructClear(session.contact_nfo)>
<!-- Now we are placing our Form Values into the Session -->
<cfset session.contact_nfo.terms= form.terms>
<cfset session.contact_nfo.Name= form.Name>
<cfset session.contact_nfo.email= form.email>
<cfset session.contact_nfo.comments= form.comments>

<!-- Now we are going to do our Error Checking first off by creating a value of nothing for the variable we are calling Errors -->
<cfparam name="errors" type="string" default="">
<!-- As we check for Errors if we find one we place it in our List to display to our user making it easy for the end-user to correct as they can see the error -->
<!-- Notice we are not validating the checkbox as it is not a required form field -->
<cfif form.Name EQ "">
<cfset errors = errors & "<li>You Forgot to enter your name!</li>">
</cfif>

<!-- Checking the email is a little more complicated so we use a regular expression to check and see if there is a email address and also is it formed properly -->
<cfif NOT REFindnocase("^[-_!a-z0-9\.]+@([-_a-z0-9]+\.)+[a-z]{2,6}$", email)>
<cfset errors = errors & "<li class=""errors"">Your Email Address seems incorrect?</li>">
</cfif>

<cfif form.comments EQ "">
<cfset errors = errors & "<li class=""errors"">You Forgot to enter a comment!</li>">
</cfif>

<cfif errors NEQ "">
<!-- Here we are collecting the Error Message generated by our code and placing them into a list for the user to see -->
<TABLE class="label1" WIDTH="300" BORDER="0" CELLSPACING="0" CELLPADDING="0" ALIGN="Center">
<TR><P> <TD ALIGN="Left" VALIGN="Top">You have some errors in your form submission, please try again<br>
<cfoutput>#errors#</cfoutput></td></P>
</tr>
</table>
<!-- Also take note that the form action is now different in that it is carrying the applications session with it which we need to make our Application.cfm file work with the action. -->
<cfoutput>
<form method="post" action="action_form.cfm?#session.UrlToken#">
<P><TABLE class="label1" WIDTH="300" BORDER="0" CELLSPACING="0" CELLPADDING="0" ALIGN="Center">

<TR> <TD ALIGN="Left" VALIGN="Top">
<!-- This is how we place our session values into the form field also important to note the because we used cfparam in the start of this page to assign a default value of "" if there was no value originally entered in the form than it will show no value here. If we did not use the cfparam tag we would now show an error as the session would go looking for a non-existant value -->
<P>Name:
  • <br>
    <INPUT TYPE="Text" class="ftforminputsmall" NAME="Name" SIZE="40" value="#session.contact_nfo.Name#"></P>
    <P>Country:<br>
    <INPUT TYPE="Text" class="ftforminputsmall" NAME="Country" SIZE="40" value="#Form.Country#"></P>
    <P>City:<br>
    <INPUT TYPE="Text" class="ftforminputsmall" NAME="City" SIZE="40" value="#Form.City#"></P>
    <P>Telephone:<br>
    <INPUT TYPE="Text" class="ftforminputsmall" NAME="Telephone" SIZE="40" value="#Form.Telephone#"></P>
    <P>E-mail:
  • <br>
    <INPUT TYPE="Text" class="ftforminputsmall" NAME="Email" SIZE="40" value="#session.contact_nfo.email#"></P>
    </P>

    <P>Comments:
  • <BR>
    <textarea cols="40" size="40" class="ftforminputsmall" rows="2" name="comments">#session.contact_nfo.comments#</textarea></P>
    <P>Booking dates:<br>
    <P>From:
    <select name="day" class="ftforminputsmall">
    <cfloop from="1" to="31" index="i">
    <option value="#i#" selected="selected">#form.day#</option>
    </cfloop>
    </select>
    <cfset monthList = "January,February,March,April,May,June,July,August,September,October,November,December" />
    <select name="month" class="ftforminputsmall">
    <cfloop list="#monthList#" index="m"> <option value="#m#" selected="selected">#form.month#</option></cfloop> </select>
    <select name="year" class="ftforminputsmall">
    <cfloop from="2006" to="2020" index="i">
    <option value="#i#">#form.year#</option>
    </cfloop>
    </select>
    </P>
    <P>To:
    <select name="dayto" class="ftforminputsmall">
    <cfloop from="1" to="31" index="i">
    <option value="#i#" selected="selected">#form.dayto#</option>
    </cfloop>
    </select>
    <cfset monthList = "January,February,March,April,May,June,July,August,September,October,November,December" />
    <select name="monthto" class="ftforminputsmall">
    <cfloop list="#monthList#" index="m"> <option value="#m#" selected="selected">#form.monthto#</option></cfloop> </select>
    <select name="yearto" class="ftforminputsmall">
    <cfloop from="2006" to="2020" index="i">
    <option value="#i#" selected="selected">#form.yearto#</option>
    </cfloop>
    </select>
    <P>
    <input name="Submit" type="submit" CLASS="ftforminputsmall" value="Submit">
    </P>
    </table>
    </form>
    </cfoutput>
    <!-- If no errors were detected we abort the previous code and show the next code section -->
    <cfabort>
    </cfif></td></tr>

  • This topic has been closed for replies.

    2 replies

    Inspiring
    April 28, 2006
    Thanks Dan it works now I was just getting confused about cfifs
    Inspiring
    April 11, 2006
    As close to the top as possible - right after the cfinclude.

    However, make sure it is defined first or your code will crash.

    Inspiring
    April 12, 2006
    Right I´ve put in the cfparams and the cfif at the top of the page after the cfinclude.
    However if the word is wrong the page still executes which it shouldn´t. It is confusing with all the cfifs but if the secret word is wrong the "sorry" message should be shown and the page stops (maybe I need cfabort or cfexit?) The message is being shown but the page continues. Here is the code:

    <cfinclude template="topnav.cfm">
    <cfparam name="session.secretWord" default="">
    <cfparam name="Form.secret_word" default="">
    <cfif form.secret_word EQ #session.secretWord#>
    Thank you for your message.
    <cfelse>
    Sorry, the secret word you entered in the field does not match the word in the image
    Please hit your back button</cfif>

    <!--- Must create this default structure or we pull an error --->
    <cfparam name="session.contact_nfo" default="">
    <cfif NOT IsStruct(session.contact_nfo)>
    <cfset session.contact_nfo = StructNew()>
    </cfif>

    <!-- Now we create default variable using cfparam -->
    <cfparam name="Form.terms" default="NO">
    <cfparam name="session.contact_nfo.Name" default="">
    <cfparam name="Form.Country" default="">
    <cfparam name="Form.City" default="">
    <cfparam name="Form.Telephone" default="">
    <cfparam name="session.contact_nfo.email" default="">
    <cfparam name="session.contact_nfo.comments" default="">

    <!-- Now we clear the Session of any Values to allow us to place our new form Values passed from our form page -->
    <cfset StructClear(session.contact_nfo)>
    <!-- Now we are placing our Form Values into the Session -->
    <cfset session.contact_nfo.terms= form.terms>
    <cfset session.contact_nfo.Name= form.Name>
    <cfset session.contact_nfo.email= form.email>
    <cfset session.contact_nfo.comments= form.comments>

    <cfparam name="errors" type="string" default="">
    <cfif form.Name EQ "">
    <cfset errors = errors & "<li>You Forgot to enter your name!</li>">
    </cfif>

    <cfif NOT REFindnocase("^[-_!a-z0-9\.]+@([-_a-z0-9]+\.)+[a-z]{2,6}$", email)>
    <cfset errors = errors & "<li class=""errors"">Your Email Address seems incorrect?</li>">
    </cfif>

    <cfif form.comments EQ "">
    <cfset errors = errors & "<li class=""errors"">You Forgot to enter a comment!</li>">
    </cfif>


    <cfif errors NEQ "">
    <!-- Here we are collecting the Error Message generated by our code and placing them into a list for the user to see -->
    <TABLE class="label1" WIDTH="300" BORDER="0" CELLSPACING="0" CELLPADDING="0" ALIGN="Center">
    <TR><P> <TD ALIGN="Left" VALIGN="Top">You have some errors in your form submission, please try again<br>
    <cfoutput>#errors#</cfoutput></td></P>
    </tr>
    </table>
    <!-- basically the same form we used in form.cfm -->
    <cfoutput>
    <form method="post" action="action_form.cfm?#session.UrlToken#">
    <P><TABLE class="label1" WIDTH="300" BORDER="0" CELLSPACING="0" CELLPADDING="0" ALIGN="Center">

    <TR> <TD ALIGN="Left" VALIGN="Top">
    <P>Name:
  • <br>
    <INPUT TYPE="Text" class="ftforminputsmall" NAME="Name" SIZE="40" value="#session.contact_nfo.Name#"></P>
    <P>Country:<br>
    <INPUT TYPE="Text" class="ftforminputsmall" NAME="Country" SIZE="40" value="#Form.Country#"></P>
    <P>City:<br>
    <INPUT TYPE="Text" class="ftforminputsmall" NAME="City" SIZE="40" value="#Form.City#"></P>
    <P>Telephone:<br>
    <INPUT TYPE="Text" class="ftforminputsmall" NAME="Telephone" SIZE="40" value="#Form.Telephone#"></P>
    <P>E-mail:
  • <br>
    <INPUT TYPE="Text" class="ftforminputsmall" NAME="Email" SIZE="40" value="#session.contact_nfo.email#"></P>
    </P>
    <P>Secret word:
  • <br>
    <cfinvoke webservice=" http://www.easycfm.com/webservices/captcha.cfc?wsdl"
    method="generateCaptcha"
    returnvariable="easyCaptcha">
    <cfinvokeargument name="height" value="100"/>
    <cfinvokeargument name="YPos" value="20"/>
    </cfinvoke> </P>

    <P><cfset session.secretWord = easyCaptcha.CAPTCHAWORD>
    <cfoutput>
    <img src="#easyCaptcha.IMAGE#" border="0"></cfoutput></P>

    <P><div="label1"> Please retype the Secret Word in the field below. This is a simple security
    matter to protect the information we receive through this form.</div></P>

    <P>
    <INPUT TYPE="Text" class="ftforminputsmall" NAME="secret_word" SIZE="40"></P>





    <P>Comments:
  • <BR>
    <textarea cols="40" size="40" class="ftforminputsmall" rows="2" name="comments">#session.contact_nfo.comments#</textarea></P>
    <P>Booking dates:<br>
    <P>From:
    <select name="day" class="ftforminputsmall">
    <cfloop from="1" to="31" index="i">
    <option value="#i#" selected="selected">#form.day#</option>
    </cfloop>
    </select>
    <cfset monthList = "January,February,March,April,May,June,July,August,September,October,November,December" />
    <select name="month" class="ftforminputsmall">
    <cfloop list="#monthList#" index="m"> <option value="#m#" selected="selected">#form.month#</option></cfloop> </select>
    <select name="year" class="ftforminputsmall">
    <cfloop from="2006" to="2020" index="i">
    <option value="#i#">#form.year#</option>
    </cfloop>
    </select>
    </P>
    <P>To:
    <select name="dayto" class="ftforminputsmall">
    <cfloop from="1" to="31" index="i">
    <option value="#i#" selected="selected">#form.dayto#</option>
    </cfloop>
    </select>
    <cfset monthList = "January,February,March,April,May,June,July,August,September,October,November,December" />
    <select name="monthto" class="ftforminputsmall">
    <cfloop list="#monthList#" index="m"> <option value="#m#" selected="selected">#form.monthto#</option></cfloop> </select>
    <select name="yearto" class="ftforminputsmall">
    <cfloop from="2006" to="2020" index="i">
    <option value="#i#" selected="selected">#form.yearto#</option>
    </cfloop>
    </select>
    <P>
    <input name="Submit" type="submit" CLASS="ftforminputsmall" value="Submit">
    </P>
    </table>
    </form>
    </cfoutput>
    <!-- If no errors were detected we abort the previous code and show the next code section -->
    <cfabort>
    </cfif></td></tr>

    <!-- In this next section we can now take our form values and do what ever we decide to do with them i.e. mailing them to ourselves, placing them into a database, or a combination of both. In this section it is wise to let the user know that his information has been received as coldfusion has no way of letting them know ---->

    <cfmail server="" username="" password="" to="" from="" subject="" type="">
    <table><tr>
    <td align="center" bgcolor="##006600" class="ftbody"><span class="ftbody2"><strong class="label1">Telephone: </strong></span></td>
    <td align="center" bgcolor="##006600" class="ftbody"><span class="ftbody2"><strong class="label1"><cfoutput>#Trim(FORM.name)#</cfoutput></strong></span></td>
    </tr></cfmail>
  • Inspiring
    April 12, 2006
    At what point do you set session.secretword?