2

Attempting to use squarebracket notation to reference a dynamic variable. (I'm looping through a set of product created by a query, creating fields for each tied to their unique SKU, if you're wondering about application)

I've narrowed it down to this chunk of code, which throws an "Invalid Expression" error when I try and run it.

<cfif FORM["QTY_" & SKU] NEQ ''>
    <div class="sopQty"><input type="number" min="0" name="QTY_#SKU#" value = "#FORM['QTY_' & SKU]#" /></div>
<cfelse>
    <div class="sopQty"><input type="number" name="QTY_#SKU#" /></div>
</cfif>

The goal is to pass the value on from the previous page if there is one, else leave the field blank. I'm thinking there's some quirk of syntax in this case that I haven't been able to figure out. Error in block below:

Type: Template

Message: Invalid Expression

Tag: CFIF

Position Line=62; Column=17

Detail Bad Expression [#FORM['QTY_' & SKU])#]

Source

60:                 <div class="sopSearch"><p>#SearchAlias#</p></div> 
61:                 <div class="sopPrice"><p>#ISBNupc#</p></div>
62:                  <cfif FORM["QTY_" & SKU] NEQ ''>
63:                     <div class="sopQty"><input type="number" min="0" name="QTY_#SKU#" value = "#FORM['QTY_' & SKU])#" /></div>
64:         <cfelse>

^ Snippet from underlying CFML source

Any suggestions?

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
EepMoody
  • 124
  • 10
  • 8
    There is an extra parenthesis `)` right before the closing `#` sign here: `#FORM['QTY_' & SKU])#` – Leigh Sep 14 '12 at 13:46
  • Fixed that, now it just throws a general error. That was a remnant of one of my many attempts to change around the syntax to hit the right way of forming it... – EepMoody Sep 14 '12 at 14:54
  • 1
    What is the new error? Can you update your post with the full error message? – Leigh Sep 14 '12 at 14:58
  • @Leigh I cannot. It seems to be a database error of some type, but due to my setup I can't review the error logs... I only get to see bad file exceptions. – EepMoody Sep 14 '12 at 16:19
  • 1
    Just a tip for the future, you can always wrap the problem code in a `cftry/cfcatch`. Then dump the error inside the catch clause. This works even if debugging is disabled and you cannot access the logs. ie ` ` – Leigh Sep 14 '12 at 16:32
  • Ah, didn't know there was a way to dump the results... thanks! I'm more comfortable with javascript myself, but the job was to learn Coldfusion and "make the damn website work"... – EepMoody Sep 14 '12 at 16:39
  • @Leigh Also, you are a goddamn wizard. I've been trying to find a way to get decent error reporting, and I've been using CfTry to isolate stuff and try to locate where a bug was cropping up. Using it with cfdump in that way has made my Friday! – EepMoody Sep 14 '12 at 20:25
  • Haha, `cfdump` is a CF developer's best friend :) You could probably rig something up using an [error handler](http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7fc4.html). Though obviously for your dev site only. – Leigh Sep 14 '12 at 20:33

2 Answers2

3

This is just an example,because I do not know where you are setting your variables, but try something like the following on your action page:

<cfset SKU = "123">
<cfset dynamic_Var = "QTY_" & variables.SKU>
<cfif IsDefined("form[dynamic_Var]")>
  <cfoutput>#form[dynamic_Var]#</cfoutput>
<cfelse>
  fail
</cfif>

Here is the submitting form:

<form name="test" action="test.cfm">
    <input type="text" name="QTY_123" value="test">
    <input type="submit" name="submit" value="submit">
</form>
JamesRLamar
  • 908
  • 1
  • 11
  • 22
  • `IsDefined` does not allow associative array notation. It would need to be `IsDefined("form.QTY_"& variables.SKU)` or alternately `structKeyExists(FORM, "QTY_"& variables.SKU)`. But I suspect the original problem was just bad syntax which prevented compilation of the page. – Leigh Sep 14 '12 at 14:39
  • In what version? I get `Parameter 1 of function IsDefined, which is now form[dynamic_Var], must be a syntactically valid variable name. ` – Leigh Sep 14 '12 at 15:22
  • True. @JamesRLamar are you are using Railo? If so, you might want to update your answer to reflect that to avoid confusion. Typically, ACF is assumed unless the question tags/content state otherwise. – Leigh Sep 14 '12 at 15:39
  • I was just about to note that. Been using Railo as of late. Thanks for the follow up ;) Spot on Adam! Sorry for the confusion @Leigh – JamesRLamar Sep 14 '12 at 16:12
  • @JamesRLamar - Actually it turned out well ;) I have been doing more ACF lately and forgot Railo allows that syntax. So it was a good reminder. – Leigh Sep 14 '12 at 16:17
  • Apparently it allows it well enough for these purposes! This actually worked perfectly. I guess this means I'm actually working on a Railo server? – EepMoody Sep 14 '12 at 16:18
  • 2
    @Artfulshrapnel - Haha, are you using Railo too? If so, be sure to tag your questions with `coldfusion` **and** `railo` to get accurate responses for your environment. – Leigh Sep 14 '12 at 16:19
  • @Leigh I actually didn't know I was using a Railo install. Neigher does my boss, who might be interested to learn that our developer used open-source software instead of the expensive software they claimed to be using... Might explain why they're so careful not to give me admin-level access to the servers. – EepMoody Sep 14 '12 at 16:22
  • 1
    To find out version you are running, do a cfdump of `#server.coldfusion#` – Leigh Sep 14 '12 at 16:26
  • 1
    Just a note (for the sake of completeness): OpenBD supports this syntax too. I might mention to Adobe that they're dragging the chain here a bit... – Adam Cameron Sep 14 '12 at 16:26
  • Looks like it's actually BlueDragon, which accepts Coldfusion input... Seems likely they built it using the open-source Railo code? Microsoft-IIS/7.5 BlueDragon.NET/7.1 – EepMoody Sep 14 '12 at 16:36
  • Open BlueDragon is different to Railo, and BlueDragon.NET is different to Open BlueDragon. All of them run CFML (with variations). ColdFusion = http://adobe.com/coldfusion Railo = http://getrailo.org OpenBD = http://openbd.org BD.NET = http://www.newatlanta.com/products/bluedragon – Peter Boughton Sep 14 '12 at 16:45
  • 1
    The biggest difference with BlueDragon.NET is that it runs on .NET rather than java like the other engines Peter mentioned. – Leigh Sep 14 '12 at 16:47
  • 3
    Yep, and also that [BD.NET costs money](http://www.newatlanta.com/products/bluedragon/product_info/pricing.cfm), where as OpenBD and Railo both have zero cost licensing fees (being GPL and LGPL respectively), so they are still using "expensive software" (though they might not need to be). – Peter Boughton Sep 14 '12 at 16:49
  • Is BD.NET still being maintained? I got the impression it'd kind of withered away? – Adam Cameron Sep 14 '12 at 17:02
  • Yeah, I was thinking the same - apparently there was a 7.1.1 [released early last year](http://blog.newatlanta.com/index.cfm/2011/2/3/BlueDragon-711-released-NET-edition) - and [noise about BD.NET 9.0 promised for later that year](http://blog.newatlanta.com/index.cfm/2011/3/15/IE9-Breaks-CFDOCUMENT-PDFs-on-BlueDragonNET#comments) which hasn't materialised. Maybe they decided to try and skip to BD.NET 11 or something. \*shrug\* – Peter Boughton Sep 14 '12 at 17:17
  • @PeterBoughton Thanks, good to know. I just knew they charge us a pretty hefty annual software licensing fee, and if they were running an open-source/free solution my boss might find that.... interesting, especially given how much work they go through to keep our in-house web designers/developers from seeing the backend. – EepMoody Sep 14 '12 at 18:06
  • @Artfulshrapnel, things that make you go "hmmmm" :) I find Railo very satisfactory and user friendly from an admin perspective as well. I'd e wiling to bet its worth looking into doing it yourself and demanding some changes. Its free to test, so it can't hurt to try. – JamesRLamar Sep 14 '12 at 19:17
  • @JamesRLamar Eh... there's a whole entrenched data exchange that links our ERP to our website, and they own the ERP system. I probably could try and do it myself, but it would take forever and we're looking to transition away from them in six months or so. (We're switching to a new more accessible ERP and Magento as an ecommerce framework. Yay!) – EepMoody Sep 17 '12 at 17:26
0

Below is also possible (and wont cause an error if the key is not defined).

<cfset key = "QTY_" & sku/>
<cfif structKeyExists(form, key) && len(form[key])>
  <!--- do something --->
</cfif>
Leigh
  • 28,765
  • 10
  • 55
  • 103
AlexP
  • 9,906
  • 1
  • 24
  • 43