• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Lesson 9. other functions > Displaying a Dynamic Message with Isdefined

Displaying a Dynamic Message with Isdefined

Often you will want a message to display on a page when a current condition is met, but only when the condition is met. For example, when you're in the process of adding products to your database, it would be helpful to know that the process has actually been completed. Currently, when you fill in the information and click the Add Product button, you are sent back to the initial Add Product screen. Your only indication that the product has been added is that you've been sent back to the starting point: It would be a more comforting user experience if they saw a message stating that the product had been added.

Open X:\Lesson9\Start\productinsertform.cfm.

This is the form that you've used to add items to the products database.

Above the form on line 107, add the following:

<cfif isdefined("url.added")> 

You are checking for the existence of the URL variable “Added”. If you have just added a record, this variable will be present, as it is passed back from the template that actually adds the record. If you are just accessing this page without adding a new product, the variable will not exist.

If the variable exists, the isdefined function will return True, and whatever is within the <cfif> block will be executed. In this case, the <cfif> block contains the process for displaying the message that will tell the user that the product information they entered into the form was added to the database.

Add your message after the <cfif> tag:

<font color="ffffff"> ** Product Added ** </font> <br> 

This is the text of the confirmation message. Of course, you can have this message say anything you like.

Add your closing </cfif> tag.

Your message is now ready for active duty, but there's another piece to the equation. The URL variable “Added” must somehow be passed to the product insert form so that your message will display. This has to be done on the same page that inserts the information to the database—productinsert.cfm—because the productinsert template returns to the product insert form template immediately after a new product is added. The productinsert template must pass back the added variable to the product insert form so it knows that the product was indeed added to the database.

Save the productinsertform.cfm file to Lesson6\Start folder in the Inetpub\wwwroot directory.

Overwrite the already existing file.

If you closed productinsert.cfm, open Lesson6\Start\productinsert.cfm.

This is the same file in which you worked with the Trim function earlier in the lesson.

At the end of the URL attribute in the <cflocation> tag add:


You have now appended the “Added” variable to the end of the URL. Now when a user submits the form to add a product, they will receive the message that the product was added.

Save the file back into the Lesson6\Start folder.

You are now ready to try it out and see the message for yourself.

Open a Web browser and type into the address window.

You will notice that the message, even though it is in the code, does not display.

Fill out the form and click Add Product.

One you have submitted the form, you will return to the same screen, only this time you will see the “product added” message.

Although you have barely scratched the surface of the functions available to you in ColdFusion, you understand how they work, how important they are, and just how powerful they can be. You will find yourself using certain functions almost daily, other functions occasionally, and some functions almost never. You can find all the information about any function in the Expression Builder in ColdFusion Studio.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint