Link to home
Start Free TrialLog in
Avatar of alicia1234
alicia1234Flag for United States of America

asked on

Need help with syntax for contstructing a URL to use in <cflocation> tag

I have this code:
<cfset urlString="modify" & #FORM.Choice# & ".cfm?recID=#URL.recID#")>
<cflocation url="test.cfm?urlstring=#urlString#">

When I execute it, my "test.cfm" page shows that the urlstring is
modifyingredients.cfm?recID=5.

YET, when I do this:
<cfset urlString="modify" & #FORM.Choice# & ".cfm?recID=#URL.recID#")>
<cflocation url=urlString>

the <cflocation> tag is trying to take me to modify.cfm.
It's obviously cutting off at the first &, which leads me to believe that I need some escape char or function to prevent it from  doing that, but I've been unable to figure out what I need to do (I'm fairly new to ColdFusion). I thought XMLFormat was the ticket but it isn't. Also tried URLEncodedFormat but that didn't help either. Same result.
Avatar of bwasyliuk
bwasyliuk

Can you try it this way:

<cfset urlString="modify" & FORM.Choice & ".cfm?recID=" & URL.recID>

Let me know if that changes anything
Avatar of alicia1234

ASKER

Thanks. You're asking me to just remove the # signs. I thought I had already tried that but just to be sure, I tried it again. It gives the same result ... the <cflocation> tag is trying to go to modify.cfm.
It's definitely that first & that is screwing it up (at least I think so!).
if it is going to Modify.cfm, then Form.choice must be equal to "".

Have you tried doing a <cfdump var="#form#"> before the cflocation?
But I know that FORM.choice isn't blank because of the test I did, right? (See my first-posted comment above.)
I don't know how <cfdump> works ... where does it dump the info to? I have debugging enabled but when I put the tag into my code, the info doesn't seem to show up anywhere. I looked in Help but it was no help. ;-(
I looked at the debugging info that I do have, and it does, indeed, look like the cflocation tag is trying to go to modify.cfm AND that the URL var recID is being passes successfully. So your thought that FORM.Choice must be blank could be correct, but why then would my test work?
<cfset urlString="modify#FORM.Choice#.cfm?recID=#URL.recID#")>

This should also work.

I've tried this with some basic options - and the

<cfset variables.choice="choice1">
<cfset url.recid=1>
<cfset urlString="modify" & variables.Choice & ".cfm?recID=" & URL.recID>

<cfoutput>#urlString#</cfoutput>


The output is "modifychoice1.cfm?recID=1" which is expected.  Have you tried feeding a dummy variable into the expression like I have done above?

Also the string:
<cfset urlString="modify" & #FORM.Choice# & ".cfm?recID=#URL.recID#")>
is not valid because of the ) on the end.
CFDUMP should be displaying the info right on the screen probabbly near the top of the screen
I realized that the problem is something else entirely ... it's a long story but I realized that I do HAVE a page named modify.cfm ... it's not even getting to the <cflocation> tag ... my page is just executing the action of the form, which is to go to "modify.cfm" (which then has the code in it to process the form if it was submitted) ...
BUT here's what's weird...
the error I am getting is the classic HTTP 404 file not found error ...
it's trying to find:
http://localhost:8500/CookingWithAlicia/admin/modifyrecipe/modify.cfm
Yet when I enter that URL into a fresh browser window, the page comes up just fine ...
I should mention that "modify.cfm" was previously in the folder "admin", but then I added the folder "modifyrecipe" below admin and moved it there. I did all the renaming and moving within Dreamweaver and I answered yes to all questions about updating links. Maybe something bad is cached somewhere? I tried closing out of DW and coming back in but that didn't help.
What the heck is going on? I'm SOOO confused!!!!  ;-(
>>> Also the string:
>>> <cfset urlString="modify" & #FORM.Choice# & ".cfm?recID=#URL.recID#")>
>>> is not valid because of the ) on the end.
I know ... it was an error in transcription to my post here ;-)
So to simplify it a bit, here is the current problem:
When I execute my page, I get the HTTP 404 error. In the browser address window there is this:
http://localhost:8500/CookingWithAlicia/admin/modifyrecipe/modify.cfm
I move my cursor to the end of the address and hit enter, and the page comes up just fine.
I had recently moved the page modify.cfm into the new subdirectory "modifyrecipe" under "admin", but the moving was all done in Dreamweaver and I did update links automatically.
I shut down and restarted my pc, hoping to clear a cache problem, but I still have the same problem.

The modify.cfm page has a form with radio buttons. You select one and hit "ok" and the form gets submitted. The form action is to revisit the same page. There is code to determine if the form has been submitted, and if it has it then executes some other code. But now I'm not even getting to that code.
right before the cflocation. try a cfDump and cfabort. that what you can see what form variables are defined and what they contain......also you can simplify the cfset for urlString....

<cfset urlString="modify#FORM.Choice#.cfm?recID=#URL.recID#">
<cfdump var="#form#">
<cfabort>
<cflocation url="test.cfm?urlstring=#urlString#">
I recreated the page from scratch.
In so doing, I've isolated it to this:

If this code is at the beginning of my page, then when I click on the "GO" button of my form (which means that this code is supposed to get executed), then I get the HTTP 404 error):
<cfparam name="URL.RecID" default="5">

<!--- Construct the URL for the page to go to if a selection was made --->
<cfif IsDefined("FORM.CWA_Modify") AND FORM.CWA_Modify eq "formModify">
<cfset urlString=URLEncodedFormat("modify" & #FORM.Choice# & ".cfm?recID=#URL.recID#")>
      <cflocation url=urlString>
<!--- <cflocation url="test.cfm?urlstring=#urlString#"> --->

</cfif>

<cfquery name="rs_Recipe" datasource="connCWArecipes">
SELECT *
FROM tblRecipes
WHERE RecipeID=#URL.recID#
</cfquery>

If that code is NOT there, clicking on "GO" brings me back in to the page without error.
SO I'm still very confused.
When I execute the page without the code (as explained in previous post) and use the debugger, I CAN SEE that "Choice" has been correctly set to "ingredients".
So FORM.Choice is defined.
I'm upping the points on this ...
Now I've isolated it to just this one line of code:

      <cflocation url=urlString>

If that line is in there, I get the HTTP 404 error; if it isn't, I get back into the page with FORM.Choices correctly set.
So I've come full circle ... the problem seems to be the urlString decoding ??

NOTE: this works (though it isn't what I want to do):

<cflocation url="modify.cfm">
OK it works now ... God only knows why it didn't before.
Here's the code that works ... so simple, really!

<cfif IsDefined("FORM.CWA_Modify") AND FORM.CWA_Modify eq "formModify">
<cfset urlString="modify" & #FORM.Choice# & ".cfm?recID=#URL.recID#">
      <cflocation url=#urlString#>
</cfif>

So from my originally-posted question, the only thing that was required was to put # signs around the "urlString" in the <cflocation> tag.
I think this now qualifies for an "I solved it myself" but if any of you disagree please let me know. I did read through all of the posts and none of them (I think!) had the correct answer.
Thanks.
Glad to hear you got it working.
I do have another open question that maybe one of you can help with  ;-)

https://www.experts-exchange.com/questions/21871516/Why-am-I-getting-an-Element-is-undefined-in-FORM-error.html
ASKER CERTIFIED SOLUTION
Avatar of Plucka
Plucka
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Plucka: thanks. Your suggested code does work. If you could explain to my in more detail why mine was "bad", I'll give you the points. I am still learning and would really like to understand "why" things are coded the way they are, not just "what" the code should be. Thanks! ;-)
Alicia,

Sure.

1. You don't need to use #'s around variables unless they are embeded in other tags or another string thus & #Form.Choice# should just be & Form.Choice
2. You were using a mixture of appending strings using & and embedding variables in strings, thus recId=#URL.recID# was changed, now both are valid methods but you were using both of them on one line you should have had one or the other, here are both complete options, see which you prefer

a: <cfset urlString="modify#FORM.Choice#.cfm?recID=#URL.recID#" />
b: <cfset urlString="modify" & FORM.Choice & ".cfm?recID=" & URL.recID />

FYI, from what i've seen most people do a as it's more natural to a CF developer

3. Your <cflocation should have the paramater enclosed in quotes, as it's possible to have a URL with spaces which would break your method. Also XHTML standard is to put all attributes in quotes.

A lot of new developers get confused with #'s i've seen it a lot they do things like

<cfset #x# = 3 />
<cfset #y# = 5 />
<cfset #z# = #X# * #y# />

Now CF is very forgiving and works a lot of stuf out but this really should be

<cfset x=3 />
<cfset y=5 />
<cfset z=x*y />

Also, note I close all the tags using /> this is another XHTML standard and also an XML standard, not enforced by Coldfusion yet, but probably will be at some point so good pratice.

Thanks!