Home > Articles > Mobile Application Development & Programming > HTML5 & Web Standards

Passing Data with Form and URL Variables

  • Print
  • + Share This
  • 💬 Discuss

From the author of

With dynamic applications, you can pass data between templates. Eben Hewitt details two common ColdFusion scopes that you can use to such advantage: FORM, which holds values passed from HTML forms, and URL, which holds parameters passed in the URL.
This article is adapted from the book Core ColdFusion 5 (Prentice Hall, 2001), by Eben Hewitt.

First, let's define scope and then quickly move on to detailed coverage of working with variables in the FORM and URL scopes.

For those who aren't familiar with the term, scope defines the visibility of a variable. That is, you can access variables in different parts of your program based on which scope they reside in.

ColdFusion defaults to the VARIABLES scope. We could reference FirstName with or without the VARIABLES scope prefix and still get the value returned to us, like this:

<CFSET firstName = "Eben">

My first name is <CFOUTPUT>#VARIABLES.firstName#</CFOUTPUT>.

All we have changed in the above code is that we are explicitly stating that we are looking for the value of the FirstName variable in the VARIABLES scope. However, this simple variable is available only to the current request and cannot be referenced for any other request.

It is a central facet of dynamic applications that you can pass data between templates. ColdFusion has a number of scopes that you can use to such advantage. Two common ones are the FORM scope, which holds values that have been passed from HTML forms, and the URL scope, which holds parameters passed in the URL.

Using Forms to Display Input Data

Here we will set up a regular HTML form and pass it to a second page called an action page. The purpose of an action page is to process the instructions given on a previous page. In this example, we have a form page that will accept user input, and an action page that will process and output that data.

Type the code in Listing 1 into a blank file in Studio.

Listing 1: An HTML Form Template

<html>
<head>
   <title>Greeting</title>
</head>

<body>

Please type your first name:<br>

<form action="actGreeting.cfm" method="post" name="frmFirstName">
   <input type="text" name="FirstName" size="20"><br>
   <input type="submit" name="Submit">
</form>

</body>
</html>

Save this file as greeting.htm, and load it in your browser. Notice that we save this file as plain HTML because there is no ColdFusion code on the page to be interpreted. We could have saved this same file with a .cfm extension, and it would have worked exactly the same way. However, we also would have forced the page to be unnecessarily processed by the CFAS.

Notice that the action attribute of the form tag specifies the name of a .cfm file that we will use to process the input. Let's create that file now.

Create a new file in Studio and type the code in Listing 2.

Listing 2: An Action Page

<html>
<head>
   <title>Welcome!</title>
</head>
<body>

Welcome, <cfoutput>#FORM.FirstName#</cfoutput>!

</body>
</html>

Go back to your browser, type your name into the form input control, and click the Submit Query button.

When I do this, I see the following displayed on the action page:

Welcome, Eben!

It's a dynamically generated page that you will use as the building blocks of many ColdFusion applications.

There are actually a number of variables that your HTML form has passed. Any time you make a FORM, you automatically get the FORM.FieldNames variable passed to the action page. This contains a comma-separated list of all of the names of form controls passed. So, for our page, FORM.FieldNames has the following value list: FIRSTNAME, SUBMIT (remember that the Submit button itself is a form control). You can leverage these variables to perform conditional processing, for example, to test whether a form has been submitted.

  • + Share This
  • 🔖 Save To Your Account
Core ColdFusion 5

From the author of

Core ColdFusion 5

Discussions

comments powered by Disqus