Skip to content

Instantly share code, notes, and snippets.

@learncfinaweek
Created November 20, 2012 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save learncfinaweek/4121103 to your computer and use it in GitHub Desktop.
Save learncfinaweek/4121103 to your computer and use it in GitHub Desktop.
Decision Making and Scopes - Hands On 6

In this hands on, we are going to refactor our code and take out any unnecessary logic we might have.

Tags Used: <cfif>, <cfelse>

  1. Open up the /www/contact.cfm file in your code editor and locate the <cfif> statement that checks the form.contactname variable. Remove the eq 0 part of the expression; as any number greater than 0 is treated as true, and the number 0 is treated as false, we do not need this additional part of the <cfif> statement. Because we only want to run the code when there is NO value passed we must negate the value by putting the word NOT before it.
  2. Remove the eq 0 part of the form.email and form.message <cfif> statements and add NOT before the len function call.
  3. Locate the <cfif> statements that checks the variable OK on or around line 120.
  4. Remove the eq false part of the expression. As a <cfif> tag is looking for a true or false value, we do not need to check the value of the variable as it is already going to be a true or false value. As we are checking if the variable is false then we need to put the ! symbol before the variable. ! is the same as NOT.
  5. Before the closing </cfif> tag of the expression that checks the OK variable, insert a <cfelse> tag.
  6. Between the <cfelse> tag and the closing </cfif> tag add the following code: <p>Form submitted successfully!</p>
  7. Your final code block should look similar to this:
    <cfif form.submitted>
    	<cfset ok = true />
    
    &lt;cfif NOT len(trim(form.contactname))&gt;
    	&lt;cfset ok = false /&gt;
    &lt;/cfif&gt;	
    
    &lt;cfif NOT len(trim(form.email))&gt;
    	&lt;cfset ok = false /&gt;
    &lt;/cfif&gt;	
    
    &lt;cfif NOT len(trim(form.message))&gt;
    	&lt;cfset ok = false /&gt;
    &lt;/cfif&gt;	
    
    &lt;cfif !ok&gt;
    	&lt;p&gt;You did not provide all the required information!&lt;/p&gt;
    &lt;cfelse&gt;
    	&lt;p&gt;Form submitted successfully!&lt;/p&gt;	
    &lt;/cfif&gt;	
    

    </cfif>

  8. Reload the contact.cfm page in your browser and enter spaces in all form fields.
  9. Click 'submit' and notice that the message "You did not provide all the required information!" message is displayed.
  10. Provide information for all the form fields and click 'submit'.
  11. Notice that the message "Form submitted successfully!" is displayed.
  12. </ol>
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment