开发者

How Can I get the URL parameter & Value in 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='#ur开发者_开发百科l.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


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:

email1=foo@bar.com&name1=Fred&email2=xxx@yyy.com&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>


   <cfset Param2 = ListGetAt(CGI.QUERY_STRING,2,"&")>


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.


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


<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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜