Skip to content

Instantly share code, notes, and snippets.

@learncfinaweek
Created November 20, 2012 21:02
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/4121096 to your computer and use it in GitHub Desktop.
Save learncfinaweek/4121096 to your computer and use it in GitHub Desktop.
Decision Making and Scopes - Hands On 4

In this hands on, you are going to validate the form data that was submitted and output any problems that might have occurred. We will access data from the form scope.

Tags Used: <cfset>, <cfif>

Functions Used: len

  1. Locate the comment that says Message Output in the /www/contact.cfm file and remove all the code that is inside the <cfif> tag just below it.
  2. Inside the <cfif> tag create a variable called OK and set it to true.
  3. Below the <cfset>, create a <cfif> statement that checks if the length of the value of form.contactname is equal to 0. To do that, use a function called len() which will return the number of characters in the provided string. Your code should look similar to this:
    <cfset ok = true />
    					
    <cfif len(form.contactname) eq 0>
    
    </cfif>
    
    	</li>
    	<li>
    		Inside the <span class="code">&lt;cfif></span> statement, set the <span class="code">OK</span> variable to false.
    	</li>
    	<li>
    		You should end up with code similar to this:
    
    <cfset ok = true />
    					
    <cfif len(form.contactname) eq 0>
    	<cfset ok = false />
    </cfif>
    
    	</li>
    	<li>
    		Create <span class="code">&lt;cfif></span> statements for the email and message fields in the form.
    	</li>
    	<li>
    		After the last <span class="code">&lt;cfif></span> statement, create a new <span class="code">&lt;cfif></span> statement that checks if the <span class="code">OK</span> value is equal to false. If it is equal to false, output the following:
    
    <p>You did not provide all the required information!</p>
    
    	</li>
    	<li>
    		Your final block of code should look similar to this:
    
    <cfif form.submitted>
    	<cfset ok = true />
    					
    	<cfif len(form.contactname) eq 0>
    		<cfset ok = false />
    	</cfif>	
    					
    	<cfif len(form.email) eq 0>
    		<cfset ok = false />
    	</cfif>	
    					
    	<cfif len(form.message) eq 0>
    		<cfset ok = false />
    	</cfif>	
    					
    	<cfif ok eq false>
    		<p>You did not provide all the required information!</p>
    	</cfif>	
    </cfif>
    
    	</li>
    	<li>
    		Reload the <span class="code">contact.cfm</span> page in your browser and click 'submit'. You should see the message "You did not provide all the required information!" displayed.
    	</li>
    	<li>
    		Fill in all the fields in the form and click 'submit'. You will notice that the message does not display.
    	</li>
    </ol>
    

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