The basal <cfif> tag lets you do article if the associated Boolean action evaluates to TRUE, but you can’t do annihilation if the action turns out to accept a amount of FALSE. This can tie your hands in situations in which you charge to acknowledge abnormally to a false condition. To allay this constraint, ColdFusion supports the accession of a <cfelse> tag to your <cfif> statement, thereby extending it to an “if-then-else” rule.
You can aggrandize the simple archetype you saw in the antecedent area to the following:
Note that <cfelse> is positioned amid the <cfif> and </cfif> tags. <cfelse> acts as a separator amid the cipher you want processed if the action is accurate and the cipher you appetite candy if the condition is false.
The <cfelse> tag does not booty any attributes, nor is there a closing </cfelse> tag. The alone actual way to use <cfelse> is as you see it in the above-mentioned example.
This added adaptability in your <cfif> statements lets you code things a little added cleanly, as approved in the afterward examples.
Recall the accomplishments and beginning blush alternative archetype from beforehand in the chapter. Back processing the user’s submission, the colorpage.cfm script had to use the <cfabort> tag to stop processing in the event that the accomplishments and beginning colors were the same. In so doing, the script never beatific the certificate anatomy tags (<html>, <head>, and <body>) to the browser. You can work around this awkward band-aid by abacus <cfelse> to the <cfif> account in colorpage.cfm, as apparent in Listing 3.8.
Here, the calligraphy is broadcast to two <cfif> statementsone to drop in the adapted <body> tag and addition to bead in the appropriate message to the user. You can, of course, handle both tasks by application a single <cfif> statement, as apparent in Listing 3.9.
Both scripts will aftermath the aforementioned achievement on the browser screen, but the version in Listing 3.9 is added able because it evaluates the action only once.
Sometimes a user needs to ample out a anatomy acreage but doesn’t for one reason or another. In this case, you can use <cfif> to determine whether the acreage was completed and, in the accident that it wasn’t, ask the user to acknowledgment to the anatomy folio and ample out that field. Consider the mailing list sign-up anatomy in Listing 3.10.
The mail architecture advice is calm by agency of a radio on. Even if the user doesn’t ample in that field, you can still accommodate a absence value for it application <cfparam>. If the user doesn’t accommodate an e-mail address, however, there’s no absence amount you can accept that will work. You accept to get that advice from the user.
You can use a <cfif> apprenticeship in the listsignup.cfm script to actuate whether the user abounding in the e-mail abode field. If she didn’t ample it in, the browser will abide a bare amount (also alleged the empty cord or a cord with no characters in it). Thus, your <cfif> should appraise the capacity of the Form.email variable and see whether it is according to the abandoned string. If so, you charge to tell the user to go aback and ample in her address. Listing 3.11 shows you how to do this.
By analytical the action Form.email eq “”, you are testing to see whether the user abounding in the appropriate field. Application this action isn’t the alone way to achieve the test, though. Addition access involves the use of the ColdFusion Len() function. Len() tells you how abounding characters a cord has, so if you canyon Len() the amount of Form.email, you can analysis whether it allotment a value of aught as follows:
The if-then-else argumentation in the above-mentioned archetype is agnate to what you saw in Listing 3.11.
There is an advantage to application the Len() action against testing the string to see whether it is according to the abandoned string. The Len() function allotment a numeric amount that is either aught (if the cord contains no characters) or some absolute cardinal (if the cord has some characters). ColdFusion interprets a aught as a Boolean FALSE and any non-zero numbers as a Boolean TRUE. Because <cfif> expects an expression that evaluates to either TRUE or FALSE, you can rewrite the above-mentioned <cfif> apprenticeship as follows:
Note actuality that you accept to about-face the two blocks of cipher to bout the condition in the <cfif> instruction. If the e-mail acreage was abounding in, Len(Form.email) allotment some absolute cardinal and is interpreted as TRUE. This compels you to booty the cipher to action back the e-mail abode is provided and move it amid the <cfif> and <cfelse> tags.
When you’re focused on architecture a dynamic, database-driven Web site, you can calmly discount acceptable grammar in the HTML pages you generate. In particular, developers generally absence application the plural forms of nouns and verbs in their output. Have you anytime apparent a book on a Web folio like the following?
The chat annal doesn’t bout the cardinal 1. Sometimes developers try to gloss over this aberration with achievement like this:
But you don’t charge to do that with ColdFusion because you can use <cfif> to actuate which words are grammatically correct. Consider the pizza adjustment anatomy in Listing 3.12.
The anatomy uses a alternation of analysis boxes to ask the user which toppings to put on the pizza. The processing calligraphy processtoppings.cfm needs to be able for two possibilities:
The user chose no toppings, in which case Form.toppings is undefined.
The user chose one or added toppings, in which case Form.toppings is anesthetized to ColdFusion as a comma-delimited account of the toppings selected.
In the additional case, you additionally accept to be acute to the achievability that the user chose alone one topping. If so, you charge to use the atypical forms of nouns and verbs. The calligraphy in Listing 3.13 shows you one way to do so.
→ To apprentice added capacity about ListLen() and added ColdFusion list functions, see “ColdFusion Lists” in Affiliate 6.
The ListLen() action tells you how abounding items are in the comma-delimited account stored in Form.toppings. If Form.toppings takes on the absence amount of the abandoned string, it is advised to be a list with aught items.
You Will Never Believe These Bizarre Truths Behind Conditional Logic Form Html | Conditional Logic Form Html – conditional logic form html
| Allowed to my personal blog, on this occasion We’ll show you regarding conditional logic form html