Displaying a Dynamic Message with Isdefined

I l @ ve RuBoard

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.

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

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

  2. 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.

  3. 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.

  4. 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 databaseproductinsert.cfmbecause 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.

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

    Overwrite the already existing file.

  6. 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.

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

     ?added=y 

    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.

  8. Save the file back into the Lesson6\Start folder.

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

  9. Open a Web browser and type http://127.0.0.1/Lesson6/Start/productinsertform.cfm into the address window.

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

  10. 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.

I l @ ve RuBoard


Macromedia ColdFusion 5. Training from the Source
Macromedia ColdFusion 5 Training from the Source (With CD-ROM)
ISBN: 0201758474
EAN: 2147483647
Year: 2002
Pages: 99
Authors: Kevin Schmidt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net