In this hands on, we are going to refactor our code and take out any unnecessary logic we might have.
- 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.
- Remove the eq 0 part of the form.email and form.message <cfif> statements and add NOT before the len function call.
- Locate the <cfif> statements that checks the variable OK on or around line 120.
- 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.
- Before the closing </cfif> tag of the expression that checks the OK variable, insert a <cfelse> tag.
- Between the <cfelse> tag and the closing </cfif> tag add the following code: <p>Form submitted successfully!</p>
-
Your final code block should look similar to this:
<cfif form.submitted> <cfset ok = true />
<cfif NOT len(trim(form.contactname))> <cfset ok = false /> </cfif> <cfif NOT len(trim(form.email))> <cfset ok = false /> </cfif> <cfif NOT len(trim(form.message))> <cfset ok = false /> </cfif> <cfif !ok> <p>You did not provide all the required information!</p> <cfelse> <p>Form submitted successfully!</p> </cfif>
</cfif>
- Reload the contact.cfm page in your browser and enter spaces in all form fields.
- Click 'submit' and notice that the message "You did not provide all the required information!" message is displayed.
- Provide information for all the form fields and click 'submit'.
- Notice that the message "Form submitted successfully!" is displayed.
</ol>