tags:

views:

1079

answers:

5

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

+5  A: 

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.

Cody Caughlan
Well, this is a good Solution, But I am looking if there is a possibility to get directly the Second Parameter and its value, instead of getting list and getting its second element..
CFUser
This is the only clan way to do it. +1
Tomalak
@Tomalak what clan? :)
Kip
+3  A: 
   <cfset Param2 = ListGetAt(CGI.QUERY_STRING,2,"&")>
kevink
I don't know if you can necessarily count on URL parameters always being in the same order in CGI.QUERY_STRING.
Al Everett
This is wrong, since it will not URL-decode the value.
Tomalak
@Tomalak I'd say whole approach is wrong, but if they want it -- they get it.
Sergii
I don't think counting on the order makes any sense either but it is what he asked for. He can URL decode the value (or not) if he wants.
kevink
+1  A: 
<cfscript>
    par2=getToken(cgi.query_string,2,"&"); // contains "par2=val2"
    par2name=getToken(par2,1,"="); // contains "par2"
    par2value=urlDecode(getToken(par2,2,"=")); // contains "val2"
</cfscript>

You could also use the listGetAt function, which is basically equivalent to getToken, with slightly different syntax.

davidcl
This is wrong, since it will not URL-decode the value.
Tomalak
Fair enough, added url decoding.
davidcl
+6  A: 

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>
Bialecki
+1  A: 

to get the list of params you can use structKeyList(url) or structKeyArray(url) then access those parameters through the url scope like #url['par1']#

<cfset params = structKeyList(url) />
<cfdump label="parameters" var="#params#" />

<cfloop index="ix" list="#params#">
    <cfoutput><div>#ix# = #url[ix]#</div></cfoutput>
</cfloop>

as others have mentioned, you really shouldn't rely on the order of parameters

fooey