Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I get the URL parameter & Value in Coldfusion?

Tags:

coldfusion

How Can I get the URL parameter & Value in Coldfusion? for Ex:-

my URL is

test.cfm?par1=val1&par2=val2&par3=val3

Is it possible to get the second parameter and its value directly?

with <cfset param='#url.par2#'> I can get value of par2, But my parameters are dynamicically generated from other page and passed to here (par2 may be next time abc2,xyz2 etc..) So I think better way is to get the parameter and Value in 2nd Possition(Possition dont change always).

Any Idea How can I get it ?

Thanks in advance

like image 570
CFUser Avatar asked Dec 14 '09 22:12

CFUser


3 Answers

You can also access the url scope as a struct, so you could get:

<cfset param2 = url['param2'] />

This is useful if you might have a naming convention for a bunch of fields. Say you're collecting names and emails like so:

[email protected]&name1=Fred&[email protected]&name2=Sally

You could write something like:

<cfloop condition="someCondition">
    <cfset email = url['email' & i] />
    <cfset name = url['name' & i] />
    <!--- Do something --->
    <cfset i++ />
</cfloop>
like image 199
Bialecki Avatar answered Sep 30 '22 01:09

Bialecki


   <cfset Param2 = ListGetAt(CGI.QUERY_STRING,2,"&")>
like image 26
kevink Avatar answered Sep 30 '22 02:09

kevink


Order of query string variables is not relevant, or your app shouldnt expect it to be relevant. I think your best bet is to have another variable which is a list of the variables in the order. Like so:

test.cfm?par1=val1&par2=val2&par3=val3&list=var1,var2,var3

Notice the presence of the new variable "list".

So you first grab the value of "list" and then takes it 2nd entry "var2" and reference that in the URL scope. You could easily abstract all of this so the names of the variables themselves dont matter. Good error handling will be necessary to guard against missing expectations.

like image 23
Cody Caughlan Avatar answered Sep 30 '22 02:09

Cody Caughlan