EasyCFM.COM ColdFusion Forums / ASP to CFML / querystring redirection

   Reply to Discussion | New Discussion << previous || next >> 
Posted By Discussion Topic: querystring redirection

book mark this topic Printer-friendly Version  send this discussion to a friend  new posts last

cfmnoob
09-27-2006 @ 3:29 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
New Member
Posts: 28
Joined: Sep 2006

Hi All: first time poster...

I'm converting some pages from ASP to CFML. I'm sniffing a QueryString var to place some conditional logic on a page, using:

<cfif Url.someQSvar is "foo">
  do something
<cfelse>
  do something else
</cfif>

This works except if there is no QS on the URL, CF throws an error...what is the best way to trap if someQSCvar exists?

JJfutbol
09-27-2006 @ 3:35 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1250
Joined: Nov 2004

First off welcome to the forums cfmnoob! Great to see you converting ASP from CF. I'm sure you will notice CF to be far more powerful and easier to read. The answer to your question is very easy. All you have to do is add a structKeyExists function call to your if statement. So you can read up on structKeyExists() here is a link: http://www.cfquickdocs.com/?sourceid=cfQDSearch1.02&getDoc=structKeyExists


<cfif structKeyExists(URL, "someQSvar") AND URL.someQSvar is "foo">
  do something
<cfelse>
  do something else
</cfif>

With the structKeyExists() function you give it the structure (all scopes in CF are structures) and then a string which contains the variable name that you want to see if it exists.

If that function call proves true, it will run the next part of the cfif statement which is testing if that variable has a specific value. If the variable does not exist in the URL struct it will not throw an error. Let me know if you have more questions. Again welcome!

----------------------------------------------------
Some free CF applications available at my site, such as the popular CFC Validator www.javier-julio.com My new site design is up!! Let me know what you all think! Working on a fully accessible Forum, which validates as full CSS and XHTML 1.0 Strict.

cfmnoob
09-27-2006 @ 3:53 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
New Member
Posts: 28
Joined: Sep 2006

wow...thanks for the detailed and fast response...I will be getting into this a little deeper soon and appreciate the hand holding.

JJfutbol
09-27-2006 @ 3:57 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1250
Joined: Nov 2004

No problem, it was a noob question that I wish when I was starting out I had known about but didn't. Just glad to share and do my part. Smile  Also make sure you are making good use of the CF documentation. http://www.cfquickdocs.com is your best friend! Use it and it will treat you well. Smile  Best of luck.

----------------------------------------------------
Some free CF applications available at my site, such as the popular CFC Validator www.javier-julio.com My new site design is up!! Let me know what you all think! Working on a fully accessible Forum, which validates as full CSS and XHTML 1.0 Strict.

cfmnoob
09-27-2006 @ 4:05 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
New Member
Posts: 28
Joined: Sep 2006

OK, quick question...that structure code works beautifully. Later on in that page I have switch statement that I converted from a VBScript "select case request.querystring("someQSvar")"

My CFML Is:
<cfswitch expression="#URL.someQSvar#">
   <cfcase value="foo">
     <cfinclude template="page1.cfm">
   </cfcase>
   <cfcase value="bar">
     <cfinclude template="page2.cfm">
   </cfcase>
   <cfdefaultcase>
     <cfinclude template="page3.cfm">
   </cfdefaultcase>
</cfswitch>

This code does NOT throw an error if there is no QS var...does "cfswitch" perform some internal error-trapping, or does it just break to the "defaultcase" if the expression can't be evaluated? Just curious!

mquack
09-28-2006 @ 12:24 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1544
Joined: Jan 2005

If 'someQSvar' does not exist in the URL scope, then CF WILL throw an error.  If you're not receiving an error, then check somewhere along the line above the switch statement for a <cfparam name="url.someQSvar" default="" />.  That will make sure that the var exists in the querystring, which will keep the switch statement error from occurring.

http://www.rachelqueensg.com

This message was edited by mquack on 9-28-06 @ 12:34 AM


Website Designed and Developed by Pablo Varando.