4

I have a search form in ColdFusion. On post, it invokes a SQL stored procedure. It returns the first 20 matching records by default, and accepts a "pagenum" parameter (in addition to many other input parameters) that allows you to indicate which set of 20 you'd like to see (for pagination). For some reason, this code achieves the desired results:

<cfset form["pagenum"] = 3 />

While this doesn't seem to have any effect at all:

<cfset form.pagenum = 3 />

I was under the impression that these 2 lines of code are exactly the same, and the only reason you'd want to use the brackets notation is if you had unusual characters in the variable that aren't allowed in the dot notation. So why am I getting different results for these 2 lines of code?

(In case it makes a difference, the stored procedure accepts all the parameters in xml format and then parses them. We have a coldfusion function that we invoke to convert the arguments struct - in this case, form - to xml and pass it into the function.)

UPDATE:

I just noticed that when I use form.pagenum, the key in the struct is in uppercase (PAGENUM), while if I use form["pagenum] it's in lowercase (pagenum). It seems that if you add a value into a struct, the key is defaulted to uppercase. And as XML is case sensitive, this may be what's throwing off the stored procedure...

froadie
  • 79,995
  • 75
  • 166
  • 235
  • You need to show us more code. How are you then *using* that variable. But you're right: the two statements should be analogous. – Adam Cameron Jan 23 '14 at 10:32

2 Answers2

3
<cfset form.pagenum = 3 />

defaults the key to uppercase (PAGENUM), causing an issue in the xml which is case sensitive and is expecting pagenum.

<cfset form["pagenum"] = 3 />

specifies the case of the key and avoids this issue.

froadie
  • 79,995
  • 75
  • 166
  • 235
2

What version of Coldfusion do you use? I tested this with CF10 and the latest railo:

<CFDUMP var="#FORM#">
<cfset form["pagenum"] = 1 />
<CFDUMP var="#FORM#">
<cfset form.pagenum = 2 />
<CFDUMP var="#FORM#">

Output of three dumps

da_didi
  • 832
  • 1
  • 7
  • 12
  • I know, I'm getting the same results in the struct as well, which is why I completely don't understand why the stored procedure is returning different results... – froadie Jan 23 '14 at 10:41
  • 2
    OH! On second thought, I just tried a simple dump and realized that when I use the dot notation, the key is `PAGENUM`, and when I use the bracket notation, it's `pagenum`. Adding a variable to a form must default to uppercase if not specified otherwise... And XML is case sensitive, isn't it? So maybe that's what's doing it... – froadie Jan 23 '14 at 10:43
  • Yes, if you need to care about the case of the key name, then you need to... care about it. And associative array notation will perserve case, whereas dot notation will (generally, but not always... it depends on the scope you're using) upper-case the key name. – Adam Cameron Jan 23 '14 at 10:58
  • 1
    @froadie, you should either answer or withdraw this question. – Dan Bracuk Jan 23 '14 at 13:34
  • Are you using CF or Railo? If you are using Railo, you can decide how the keys are stored, so you can fix the upper case issue. With CF, using dot notation will (usually) uppercase it for you as Adam said. – Gavin Pickin Jan 23 '14 at 15:46