I am sending a value 开发者_Python百科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.
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:
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:
...and finally:
cs_form['CONfusion']; // def
cs_form['conFUSion']; // abc
cs_form['CONFUSION']; // Error, undefined in java.util.HashMap
Use #URL.variable#
for GET.
Use #FORM.variable#
for POST.
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.
You can access them using #url.variable#
. For example, in PHP you might have $_GET['id']
and in CF you would have #url.id#
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.
精彩评论