| 1: |
When exactly should variables or expressions be enclosed within # characters?
|
|
In CFML, # is used to mark the start and end of a ColdFusion variable or expression so as to distinguish it from strings. So, in a block between <CFOUTPUT> and </CFOUTPUT> (which is essentially a big string), all variables and expressions must be enclosed in between # characters. However, within tags, data is assumed to be variables and expressions and not strings, and so <CFSET x=y+1> does not need # characters around x and y. The rule simply is, only use # characters where ColdFusion assumes that data is a string.
|
|
| 2: |
When I use # in colors (for example, #FF00FF) ColdFusion throws an error. How can I correct this?
|
|
The # character has a special meaning to ColdFusion, it is used to delimit a variable or an expression. If ColdFusion encounters a # character it expects to find a matching one at the end of the variable or expression. To use a # character as a # character you must escape it using double # characters. So the example in the question would need to be ##FF00FF.
|
|
| 3: |
Why do I see #var# in my generated page instead of the actual variable value?
|
|
This is almost always caused by failing to enclose expressions within <CFOUTPUT> and </CFOUTPUT> tags.
|
|
| 4: |
What is Verity used for?
|
|
Verity is a full text search engine. Verity is used to perform very flexible searches for specific text, and to do so very quickly. Verity (in ColdFusion) is used in two ways: 1) You can use Verity to search files on disk for specific content (text files, Microsoft Word files, and more), 2) Verity can also be used to search for specific text in database records (especially memo or text fields which can be rather lengthy). Verity searches are performed using the ColdFusion <CFSEARCH> tag.
|
|
| 5: |
Can CFML conditional tags be nested?
|
|
Yes, CFML conditional and flow-control tags (like <CFIF> and <CFLOOP>) can indeed be nested (placed within other tags). But, deep nesting can impact performance, so don't nest unnecessarily.
|
|
| 6: |
Is there a way for a file to know it's own file name?
|
|
There are a few ways:
cgi.script_name refers to both the path and file name, so if your file was in a folder under the webroot called stuff, and was named foo.cfm, the value of cgi.script_name would be /stuff/foo.cfm.
Another cgi variables, cgi.cf_template_path, represents the complete path of the current file. You can also call the CF function, getCurrentTemplatePath. If you only want the file name from these two values, use the getFileFromPath function.
|
|
| 7: |
I have a page with cfincludes for the header and footer. I want to add a cfmail form in the footer for contacting the webmaster. Once the e-mail is sent I want to add a link to the full URL of the page the user was on when originally clicking to submit comments (i.e. "return to the page I was at"). How do I record the page I was at?
|
|
When you create your link to the feedback page, simply append the current file name. For example:
<cfset ref = cgi.script_name & "?" & cgi.query_string>
<a href="feedback.cfm?ref=#urlEncodedFormat(ref)#">
Your feedback form will look for the value, url.ref, which is the url (including any query string information). You should be sure to store the value as a hidden form field so that the value doesn't expire. After the form is submitted you could then simply use cflocation to push the user back, or provide a dynamic link back.
This is also useful in cases where you have a login form. You could modify the technique above so that when a user is forced to logon, they then get directed to the page they had originally requested.
|
|
| 8: |
When using comma delimited ColdFusion lists, why are empty elements ignored?
|
|
ColdFusion lists are not CSV comma-delimited lists and do not behave as CSV lists do. ColdFusion ignores empty list elements, so ListLen("a,b,,d,e") will return 4 (not 5), and ListGetAt("a,b,,d,e", 5) will throw an error. To convert a ColdFusion list to a CSV list, use Replace() and replace all ",," (comma comma) with ", ," (comma space comma) - a space is a valid list member value and ColdFusion will not ignore it.
|
|
| 9: |
When and why are quotes needed in <CFQUERY> SQL statements?
|
|
All content in between <CFQUERY> and </CFQUERY> is sent to the database via the database driver as is. Unlike CFML, SQL is not typeless, and so SQL statements must be complete and valid as per SQL syntax specifications. All strings (or variables being used as string values) must be enclosed within singe quotes. Similarly, all numeric values must not be within quotes, and all dates and times must be formatted as valid date and time values.
|
|
| 10: |
Should I use the <CFINSERT> and <CFUPDATE> tags?
|
|
If you can, absolutely, there is no reason not to. But be aware that these tags do have significant limitations - they only work with FORM fields, they require that the FORM fields be named the same as the table columns, you cannot do any processing or manipulation of the values, and when updating the row primary key must be present as a FORM field (possibly as a hidden field).
|
|
| 11: |
My mail server requires a login to send mail. How can I send mail from ColdFusion using a login?
|
|
Support for SMTP login was added to ColdFusion MX 6.1. You may either pass the login information in your <CFMAIL> tag using the USERNAME and PASSWORD attributes, or specify it in the ColdFusion Administrator using the syntax: user:password@hostname.
|
|
| 12: |
Is it possible to generate an e-mail message with both text and HTML versions in the same message?
|
|
This is possible as of ColdFusion MX 6.1 via the new <CFMAILPART> tag. The following code snippet demonstrates the use of this new tag:
<CFMAIL TO="..." FROM="..." SUBJECT="...">
<CFMAILPART TYPE="text">
This is the text version.
</CFMAILPART>
<CFMAILPART TYPE="html">
<B>This is the HTML version.</B>
</CFMAILPART>
</CFMAIL>
|
|
| 13: |
I am sending e-mail to my mailing list, and need a way to easily track mail delivery failures or rejections. How can I do this?
|
|
<CFMAIL> has a FAILTO attribute which can be used to specify the address to which failure notifications should be sent. Using this attribute you can specify an alternate address (one other than the sender) which will collect all failure notices:
|
|
| 14: |
How can I obtain the date, time, or size of a file?
|
|
<CFDIRECTORY> is used for directory access and manipulation. ACTION="list" returns the date, time, size, and file attributes.
|
|
| 15: |
Can ColdFusion read or access files on the client computer?
|
|
No, ColdFusion runs on the server, it can do nothing on the client at all. In fact, the only information that ColdFusion receives from the client is what the browser on the client sends – nothing more. So ColdFusion cannot access client side Access files, Word DOC files, images, or anything else unless those are explicitly uploaded to ColdFusion.
|
|
| 16: |
Can ColdFusion determine the user that a client is logged in as?
|
|
Developers would love to be able to dynamically determine client workstation login information so as to use this for access control within their own applications. Unfortunately, this information is not available to server side technologies like ColdFusion (if it were, that would present an enormous security risk). The only way to get this information is to have some application running on the client (maybe a Java applet or an ActiveX control, for example) and have it return this information to you.
|
|
| 17: |
How can I transfer files between a client browser and ColdFusion?
|
|
The safest way to transfer files from a client computer to ColdFusion is to use HTTP file upload. ColdFusion does have the ability to access files via HTTP and FTP but these only work if the computer with the files is running an HTTP or FTP server, and most clients won't be.
|
|
| 18: |
How do I create a form to be used for file uploading?
|
|
You need to set create a form, set the <FORM> ENCTYPE attribute to "multiform/form-data", and then add a form field of TYPE="file". When the form is displayed it will contain a field and button for file selection, and when the form is submitted the file will be uploaded to ColdFusion. An example of this is shown below:
<FORM ACTION="process.cfm"
ENCTYPE="multipart/form-data"
METHOD="post">
File: <INPUT TYPE="file" NAME="FileToUpload">
<BR>
<INPUT TYPE="submit" VALUE="Upload">
|
|
| 19: |
How do I access and process uploaded files?
|
|
Files uploaded in HTML forms are accessed using the <CFFILE ACTION="upload"> tag. The tag allows you to save the uploaded file, rename it, accept only certain file types, and more.
|
|
| 20: |
What is the REQUEST scope?
|
|
REQUEST is similar to VARIABLES in that it is tied to a single request only and contents persist only for the duration of page processing. The difference between REQUEST and VARIABLES is that REQUEST is also visible to other files being processed in the REQUEST, like Custom Tags.
|
|
| 21: |
How do you make sessions close when a browser closes?
|
|
In ColdFusion MX, this can be achieved very easily. Simply go to your ColdFusion Administrator, select Memory Variables, and add a checkmark by the Use J2EE session variables.
In earlier versions, ColdFusion SESSION variables use cookies (named CFID and CFTOKEN) to uniquely identify clients. By default, these cookies are stored to disk and so sessions persist even after a browser closes. To prevent this behavior, simply rewrite the cookies so that they are browser cookies (cookies that expire when a browser closes). The following code snippet writes the two browser cookies:
<CFCOOKIE NAME="cfid" VALUE="#SESSION.cfid#">
<CFCOOKIE NAME="cftoken" VALUE="#SESSION.cftoken#">
|
|
| 22: |
When should I use <CFLOCK>?
|
|
The purpose of <CFLOCK> is to control concurrent access to a block of code or a scope. Prior versions of ColdFusion (pre CFMX) had a limitation in that unlocked concurrent access to shared scopes (like SESSION or APPLICATION) could result in memory corruption and thus server failure. This is no longer the case in ColdFusion MX, but there are still valid reasons to use <CFLOCK>. Firstly, any code that is not multi-user safe (third party tags, access to file system files, etc.) should be locked to ensure sequential (as opposed to concurrent) access. Secondly, even though not locking access to shared scopes will not cause any server problems, the lack of <CFLOCK> use may cause application irregularities. For example, if an application updates a set of APPLICATION variables and access is not locked, it would be possible for users to access some old variables and some new ones. This may or may not be a problem, that depends on the application, if this is a problem then you’ll want to use <CFLOCK>.
|
|
| 23: |
What is the difference between EXCLUSIVE and READONLY locks?
|
|
<CFLOCK> supports two types of lock, EXCLUSIVE and READONLY. An EXCLUSIVE lock prevents access by any other code. A READONLY lock is not actually a lock, it is more of a flag that marks a block of code that will only be executed if there is no active EXCLUSIVE lock. It is important to use READONLY locks in your code as without them ColdFusion could execute that code even if there were an EXCLUSIVE lock active – ColdFusion would have no way to know that the code should not be executed yet.
|
|
| 24: |
How can I create a variable using a variable in the variable name?
|
|
The trick is to enclose the entire variable name in quotes, and then use your variable within the name enclosed within # characters. An example of this is shown below:
<!--- Set i --->
<CFSET i=1>
<!--- Create product1 variable dynamically --->
<CFSET "product#i#"="apple">
<!--- Display product1 variable --->
<CFOUTPUT>#product1#</CFOUTPUT>
|
|
| 25: |
How can I obtain the value out of a dynamically named variable?
|
|
Variable names can be constructed dynamically and evaluated using the Evaluate() function. An example of this is shown below:
<!--- Set i --->
<CFSET i=1>
<!--- Display product1 variable --->
<CFOUTPUT>#Evaluate("product#i#")#</CFOUTPUT>
|
|
| 26: |
How can I obtain the list of columns in a retrieved query?
|
|
Every query contains a member named columnlist which can be accessed as query.columnlist.
|
|
| 27: |
How can I obtain the list of any received FORM fields or URL parameters?
|
|
URL and FORM are ColdFusion structures, and may be used as any other ColdFusion structures. For example, contents may be dumped, members may be accessed directly, and <CFLOOP> can be used to loop through URL or FORM members.
|
|
| 28: |
How can I directly access specific query data?
|
|
For direct access to specific query rows and columns, access them as if they were structures and arrays. You can refer to a specific column of a specific row as #query.column[row]#. For example, if you wanted the "price" column for the fifth row of a query named "products" you could use the expression #products.price[5]#. This technique is especially useful when looping through query results to perform some processing.
|
|
| 29: |
How can I generate printable PDF output using ColdFusion?
|
|
PDF generation is supported as of ColdFusion MX 7 using the new <cfdocument> tag.
|
|