As previously mentioned, all variables passed using the Post method will be FORM scope variables and should be referenced using the FORM scope prefix. There also are a few tricks that bright, shiny, new ColdFusion programmers should be aware of when processing formsnamely, that not all form fields will always pass information to the action page. Check boxes and radio buttons will only pass a value if they have been checked. If we add the following line of code to add a check box to our form, we would end up with the form shown in Figure 5.3. Figure 5.3. The form with a check box. Include me on mailing list: <INPUT TYPE="checkbox" NAME="MailList" VALUE="Yes"> The code adds a check box to ask the user if he would like to be on our mailing list. The code creates a FORM variable named MailList and, if checked, passes a value of Yes. NOTE If we had not specified a value for the for this check box by using the VALUE attribute (VALUE="Yes"), the check box would pass a default value of On if checked. By adding the following line of code to our action page, we can access this new variable's value. <B>Mailing List: </B>#FORM.MailList#<BR> Figure 5.4 shows the display that will result. Figure 5.4. Action page results with the check box value. The trick comes if the user decides, God forbid, that he doesn't want to be on our mailing list. If he submits the form with the check box empty, as shown in Figure 5.5, the form does not submit a value of Off or No as you might expect. The form does not submit a value at all. Figure 5.5. The form with an empty check box. "Big deal," some of you might say (Shame on you!). The big deal comes when we run the action page, which tries to reference the FORM.MailList variable. Because the form page did not pass a value for FORM.MailList, the variable does not exist. You might remember from Step 2 that if you try to use a variable that has not been set, ColdFusion will give you a big fat error like the one in Figure 5.6. As you can see in Figure 5.6, ColdFusion tries to output the action page we coded. However, when it gets to the line that uses the variable FORM.MailList, ColdFusion throws the error Element MailList is undefined.... Because the form did not pass the variable, it does not exist on this page. Figure 5.6. The error resulting from the undefined check box value. Because you never know whether a user is going to select a check box or radio button, this becomes a problem. There are a couple of easy ways to get around this problem. One way is to use the <CFPARAM> tag on the action page. You might remember from Step 2 that <CFPARAM> checks to see whether a variable exists, and if it doesn't, it creates the variable and gives it a default value. We could put a <CFPARAM NAME="FORM.MailList" DEFAULT="No"> tag at the top of our action page. This tag would check for the existence of the FORM.MailList variable. If it exists (if it has been passed by the form), fine, do nothing and carry on with the code. If it does not exist (if it hasn't been passed by the form), create the variable and assign it a value of No. This way, we can always be sure that the variable exists before we try to output it. Another way to solve the problem is to check for the existence of the variable using conditional logic and the ColdFusion IsDefined() function. NOTE If you use METHOD="Get" to pass your form information to an action page, the variables will not be available in the FORM scope. You will have to access the variables (which will be passed via the URL) using the URL scope and the URL.FieldName syntax. It is recommended that you pass form variables using METHOD="Post". |