Calling a variable with a variable in its name - coldfusion?

后端 未结 2 1050
半阙折子戏
半阙折子戏 2021-01-22 10:18

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 uniqu

相关标签:
2条回答
  • 2021-01-22 11:05

    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>
    
    0 讨论(0)
  • 2021-01-22 11:12

    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>
    
    0 讨论(0)
提交回复
热议问题