user400749
user400749

Reputation: 195

ColdFusion get method

I am sending a value of a variable from an http url to another cfm page, but I am not sure how to get that value on the other page. In php we use $_GET['variable']; I am not sure what is the equivalent of that in ColdFusion.

Upvotes: 3

Views: 3055

Answers (5)

Niall
Niall

Reputation: 138

I've done this in coldfusion 7 before.

You can use the cgi.query_string value to get the query string and then split as follows:

httpGetValues = createobject('java','java.util.HashMap').init();

nameValuePairs = cgi.query_string.split('&');
for( i=1; i lte arraylen(nameValuePairs); i = i + 1 ){
    pair= nameValuePairs[i].split('=');
    key = URLDecode(pair[1], "UTF-8");
    value = URLDecode(pair[2], "UTF-8");
    httpGetValues[key] = value;
}

Make sure that you decode the values.

Upvotes: 0

Ken Redler
Ken Redler

Reputation: 23943

ColdFusion has the option of accessing these variables very much like you're doing in PHP:

PHP:

$foo = $_GET['variablename'];
$bar = $_POST['variablename'];

CFScript:

foo = URL['variablename'];
bar = FORM['variablename'];

CFML:

<cfset foo = URL['variablename']>
<cfset bar = FORM['variablename']>

Edit: Discussion of Form Scope Case Insensitivity, and a Workaround

ColdFusion will (helpfully?) convert all form fieldnames to uppercase in the form scope. In cases where a fieldname is repeated, the multiple values will be merged into a single comma-separated value. When you don't have control over the form itself, this can lead to frustration.

Given the form:

<form name="main" action="handler.cfm" method="post">
  <input type="text" name="conFUSion" value="abc" />
  <input type="text" name="CONfusion" value="def" />
  <input type="submit" name="Submit" />
</form>

The form scope on the receiving page would look like this:

Regular Form Scope

But you can use gethttprequestdata().content to directly access the original form case-preserved fields and values as posted:

conFUSion=abc&CONfusion=def&Submit=Submit

Since ColdFusion structs are case-insensitive, we can't simply parse this string into a regular struct. Instead we can turn to java.util.HashMap, which is very much like a ColdFusion struct, but does preserve case:

arFormscope = gethttprequestdata().content.split('&');
cs_form = createobject('java','java.util.HashMap').init();
for( i=1; i<=arraylen(arFormscope); i++ ){
  arElement = arFormscope[i].split('=');
  key = arElement[1];
  value = arElement[2];
  cs_form[key] = value;
}

Dumping the cs_form hashmap, we get:

enter image description here

...and finally:

cs_form['CONfusion']; // def
cs_form['conFUSion']; // abc
cs_form['CONFUSION']; // Error, undefined in java.util.HashMap

Upvotes: 13

Todd Sharp
Todd Sharp

Reputation: 3355

There are enough good answers, but I'll just add that one of the nice things about associative array notation for accessing struct keys is that you still have access to keys that are syntactically invalid. So if you created a page called test.cfm like such:

<cfdump var="#url#">
<cfoutput>
#url['bad bad var name']#<br />
</cfoutput>

And called it like such:

http://localhost/test.cfm?bad bad var name=foo

You would see 'foo' output on the page.

But if you tried this:

<cfdump var="#url#">
<cfoutput>
#url.bad bad var name#
</cfoutput>

You would see:

Invalid CFML construct found on line 3 at column 10. ColdFusion was looking at the following text:

bad

Because variable names can't contain spaces.

Of course no one would intentionally name a URL parameter with a space (I hope), but this comes in handy with things like queries and external data is out of the developers immediate control.

Upvotes: 3

CrazyPyro
CrazyPyro

Reputation: 3617

Use #URL.variable# for GET. Use #FORM.variable# for POST.

Upvotes: 4

Sean Walsh
Sean Walsh

Reputation: 8344

You can access them using #url.variable#. For example, in PHP you might have $_GET['id'] and in CF you would have #url.id#

Upvotes: 2

Related Questions