Page 1 of 1

Tutorial to submit a form and then access those form attributes Rate Topic: -----

#1 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 2024
  • View blog
  • Posts: 3,608
  • Joined: 13-January 08

Posted 25 September 2015 - 08:20 AM

View Postatraub, on 25 September 2015 - 10:16 AM, said:

I'd love a very simple tutorial for submitting a form and then accessing those form attributes server side. We use ColdFusion at my office and I'm still learning :smartass:/>/>

If you're familiar with HTML forms then you know 90% of what you need to know to access form values server side from a form submission.

For the purposes of this example (not really a tutorial because the subject is probably too short for such) we'll have 2 CFM pages: form.cfm and formReceiver.cfm. Below is form.cfm:
<form name="testForm" id="testForm_ID" action="formReceiver.cfm">
	<input type="hidden" name="formVar1" id="formVar1_ID" value="this 1">
	<input type="text" name="formVar2" id="formVar2_ID" value="this 2">
	<input type="Submit" name="formSubmit" id="formSubmit_ID" value="Send">

Nothing special, right? Almost exactly like a regular HTML form page. The differences are that we are specifying submission to a .cfm page in the form tag's action attribute (formReceiver.cfm) and that I have given each form element a name AND an ID (will be handy to note this later). So, bringing up this form, you see a text input field with the prefilled value of "test 2" and a Submit button labeled as "Send". You also have a hidden form field with a value of "this 1". Pressing the "Send" button submits the form to the formReceiver.cfm page which is shown below:
<cfdump var="#form#">

Right now, that is all we're going to look at. There is just a CFDUMP tag and what it's doing with its "var" attribute is dumping the contents of the "form" variables scope to the screen. If you're following along at home, what you'll see is an array with the name of the form variable and it's attendant value from the form you just submitted. So, you should see form variables entitled formVar1 and formVar2 and concurrent values of "this 1" and "this 2". So, handy right? That said, it can get handier. Try modifying formReceiver.cfm to show this:
<cfdump var="#form#">

	form.formVar1: [#form.formVar1#]<br>
	form.formVar2: [#form.formVar2#]

In this example, you still have the CFDUMP of the entire form variables scope but now you also have a CFOUTPUT section where you have referenced the individual form variables themselves. What this demonstrates is that each variable in the form scope is accessible and addressable by prefacing the variable's name with that specific variable's scope. For example, formVar1 is the name of the variable, "form" is that variable's scope. In Coldfusion, this is an important consideration because Coldfusion encompasses many different variable scopes and each variable scope can have identically named variables but with different values and sometimes entirely different content (lists, arrays, structures or entire objects rather than a text string). So, using formVar1 as an example, you can have a page that presents formVar1 but across a variety of variable scopes like so:

  • form.formVar1
  • variables.formVar1
  • session.formVar1
  • cookie.formVar1
  • server.formVar1
  • application.formVar1
  • cgi.formVar1
  • client.formVar1
...and so on.

The reason explicitly referencing the scope is important is because Coldfusion allows you to reference a variable WITHOUT explicitly stating its scope. However, when you do that, the CF server has a priority list of variable scopes that it rolls through until it finds one that contains the variable name you referenced. If you intended to reference form.formVar1 but had a variable called session.formVar1 and if the CF server gave the session scope priority over the form scope (TBH, I don't know if it does or not and the priorities have been known to be shuffled from one version of Adobe CF to the next) and you only referenced the variable as "formVar1" you might end up with the value from the session scope variable rather than the form scope variable. So, in CF, scoping is not only important it's best practices.

In any event, you have your form variable handy and available and, once on the formReceiver.cfm page, you can mess with and manipulate your form variables (as well as variables from other scopes you may have available) to your heart's content.

Finally, I mentioned earlier that I had given each form element a name and an ID but, as you can see, I named the IDs differently from the elements' names. Note that what gets passed to CF for variable reference purposes is the name attribute and not the ID attribute. This is handy to know as, occasionally, you do get form elements whose names and ID values don't match. IDs are used by developers to reference and manipulate those elements in the DOM, usually by Javascript, CSS, and various JS frameworks and the like. Handy to keep in mind, is all.

Finally, getting the form values server side and having the ability to reference and use them is just the tip of the CF iceberg. Your best resource for CF related code and all other things Coldfusion is our own CF Resources thread in the CF forum here on DIC. Very handy one stop thread for all your CF resource needs.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1