Copy link to clipboard
Copied
I am trying to set up a loop that sets up a list of variables based upon a list, by looping over the list and setting the session.NAME:
<CFLOOP list="#fieldnames#" index="fieldname">
<cfset session.#fieldname# = "1">
</CFLOOP>
It does not seem to like session.A_CF_VARIABLE . so session.#fieldname# does not work, I get an error
A CFML variable name cannot end with a "." character.
The variable session. ends with a "." character. You must supply an additional structure key or delete the "." character.
Do I have to wrap it a different way?
Thanks
Mark
You were write, I had the wrong scope format, I managed to knock up a little test that worked. I just needed to use form[fieldname]
What I did notice is that I was using 1_email for the form name, when I came to hard code that for testing purposes, a CFSET failed, saying it was not a valid name, so I'm going to have to change the format to email_1 etc, no big deal. Here's how I managed to get it to work
<CFIF NOT #IsDefined('url.write')#>
<form action="write.cfm?write=1" method="post">
<input type=
...Copy link to clipboard
Copied
Since session is a structure, you should be using array notation
ie session[ fieldname] = "some value"
Copy link to clipboard
Copied
ahh. great. that fixed the loop problem. My second problem now appears that I am not picking up the correct data.
My test data has a form post with field names EMAIL and GENDER, and then I was trying to write session.email = VALUE_OF_FORM_FOR_EMAIL .. session.gender = VALUE_OF_FORM_FOR_GENDER, but what I am actually doing is writing the values EMAIL and GENDER into the session. so session.email has the value EMAIL.. and not what I typed into the form.
I guess it's because the fieldname in the index is also used as the name in the form and it's confusing it, so it goes for the wrong one, not the value from the form.
the alternative would be to append something to the name of each field in the form.. so email is F_email and F_gender... although i tried this and using <CFSET session[fieldname] = "F_#fieldname"> but that threw an error, maybe just a formatting issue. I'd prefer to try and make it work without appending anything to the form names
Here's some test code that has the form fields hard coded -->
<CFSET fieldnames="email,gender">
<CFSET email = "test@nosuchdomain.com">
<CFSET gender = "M">
<CFLOOP list="#fieldnames#" index="fieldname">
<cfset session[fieldname] = '#fieldname#'>
</CFLOOP>
Thanks once again for the help
Mark
Copy link to clipboard
Copied
so session.email has the value EMAIL.. and not what I typed into the form.
Hint: FORM is a structure too
Copy link to clipboard
Copied
exactly.
I read the "hint" , but still couldn't figure it out.. tried [ ] around it
Copy link to clipboard
Copied
Did you add the scope? Because array notation works for both setting and getting values
ie scopeName[ "keyName"]
You might also want to read the docs on using associative notation (also called structure notation).
http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSf01dbd23413dda0e-2a6ba8891200fcc8c06-7ffd.html
http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7fb2.html
Copy link to clipboard
Copied
if this info is coming from a form on a previous page and the value was NAME="email" what would the scope be?
Copy link to clipboard
Copied
It is just like any form submission. When using method=get, data is passed in the URL scope. If method=post then FORM.
Copy link to clipboard
Copied
I actually tried form.email but it did not seem to be able to find it
Copy link to clipboard
Copied
I actually tried form.email but it did not seem to be able to find it
Dump the FORM scope to see if it contains a field by that name. If it does, the new syntax is probably wrong. Can you post your latest code?
Copy link to clipboard
Copied
You were write, I had the wrong scope format, I managed to knock up a little test that worked. I just needed to use form[fieldname]
What I did notice is that I was using 1_email for the form name, when I came to hard code that for testing purposes, a CFSET failed, saying it was not a valid name, so I'm going to have to change the format to email_1 etc, no big deal. Here's how I managed to get it to work
<CFIF NOT #IsDefined('url.write')#>
<form action="write.cfm?write=1" method="post">
<input type="text" name="email_1">
<input type="submit">
</form>
<CFABORT>
</CFIF>
<CFSET fieldnames="email_1">
<!--- SET ALL SESSION DATA --->
<CFLOOP list="#fieldnames#" index="fieldname">
<cfset session[fieldname] = form[fieldname]>
</CFLOOP>
<CFOUTPUT>#session.email_1#</CFOUTPUT>
Many thanks (again) for all of you help!
Mark
Copy link to clipboard
Copied
a CFSET failed, saying it
was not a valid name
Yep. Variable names cannot start with a number when using standard dot notation. You have to use array notation instead to access the value ie cfset value = FORM["1_email"]
http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7fd3.html
Copy link to clipboard
Copied
ahhh .gotcha... already flipped it around
Copy link to clipboard
Copied
Ummm.. I wrote another script to check all the session variables to see if I already have ALL of them in a session, however it throws an error -->
<CFSET flag="0">
<CFLOOP list="#fieldnames#" index="fieldname">
<CFIF NOT #ISDefined(session[fieldname])#><CFSET flag="1"></CFIF>
</CFLOOP>
ERROR: Parameter 1 of function IsDefined, which is now test@nosuchemail.com, must be a syntactically valid variable name.
I guess I have another formatting error?
Copy link to clipboard
Copied
ERROR: Parameter 1 of function IsDefined, which is now test@nosuchemail.com, must be a syntactically valid variable name.
I guess I have another formatting error?
Guess so, not surprising that you cannot use an email address as a variable name.
Copy link to clipboard
Copied
I tried single quotes around the session[] part to no avail, I want to check if the session exists, not what is in it.
Copy link to clipboard
Copied
>> I want to check if the session exists, not what is in it.
CFIF NOT #ISDefined(session[fieldname])#
But that is not what you are telling the code to do. Take a step back and look at what the code is actually doing rather than just trying randomly trying stuff.
Copy link to clipboard
Copied
OK, I tried a hard coded session variable... session.email_1 and it does not throw an error, I added the single quotes that I believe I should have had
<CFSET flag="0">
<CFLOOP list="#fieldnames#" index="fieldname">
<CFIF NOT #ISDefined('session.email_1')#><CFSET flag="1"></CFIF>
</CFLOOP>
Then I changed it to the fieldname so I could loop over all variables in the CFLOOP
<CFSET flag="0">
<CFLOOP list="#fieldnames#" index="fieldname">
<CFIF NOT #ISDefined('session.[fieldname]')#><CFSET flag="1"></CFIF>
</CFLOOP>
It throws an error:
Parameter 1 of function IsDefined, which is now session.[fieldname], must be a syntactically valid variable name.
Wronf format ?
Copy link to clipboard
Copied
Ask yourself this - what does the IsDefined() expect versus what you are actually passing in?
Copy link to clipboard
Copied
Well the way I wrote it, what I was expecting was for it to figure out if a previous form has actually written that session variable already, so does it exist? The idea is that if a form requires fields that I have already collected then I can skip it and move to the next.
I want it to look at whether session.email_1 exists or not, session.gender_2 exists and so on, if ANY of them do NOT exist then I display the form, and populate the fields that I do have with the sessions that do exist
Copy link to clipboard
Copied
I'll give you a hint - try cfoutputting the "fieldname" variable rather than putting it into the isDefined() function. I don't think you're doing what you think you're doing.
Copy link to clipboard
Copied
Yes, I understood the goal. But my point was obviously it is not doing that. Meaning your code is not actually doing what you think it is, as Owain already said. Whenever that happens you need to debug your code / dump your variables so you can see what is actually happening .. not what you think is happening.
Copy link to clipboard
Copied
understood, although I have to admit I have stared at this for some time and can't seem to figure it out.
it looks like my formatting is certainly wrong, possibly on top of the logic
looking for clues
Copy link to clipboard
Copied
Knowing how to find issues in your code is as important as knowing how to write it in the first place, hence personally (and it seems cfSearching is the same) I would rather help you figure it out rather than just give you the answer.
As I suggested before, do not guess what's going on. *Know* what's going on. Change your loop so that on each iteration it is putting out the contents of the "fieldname" variable to screen, not to some logic you cannot see. I suspect then you'll realise what's going on.
Copy link to clipboard
Copied
Ummm... OK, is this going to be so obvious that it's embaressing ... It must be staring me in the face
I changed the code to list the variables, the first one in the list did exist, do it displayed the content of session_email_1 as expected, the 2nd variable session.address_2 did not exist and therefore as expected it was unable to display it so I got an error
Element address1_2 is undefined in a Java object of type class coldfusion.runtime.MemorySessionScope.
Code
<CFLOOP list="#fieldnames#" index="fieldname">
<CFOUTPUT>#session[fieldname]#</CFOUTPUT>
</CFLOOP>
You're going to have to throw me another clue 😉
Mark