How do I grab the variable NAME and not the VALUE of the variable in ColdFusion? - variables

The basic idea is this: I have a form that generates form fields dynamically so let's say there are 5 evens people can sign up for (they all cost $10) then those 5 evens will be displayed. Like this:
<tr>
<th><label>#SeminarWisTitle#</label></th>
<td>
<label><input type="checkbox" name="#SeminarWisID#" value="10.00" onclick="CheckChoice(this);" onfocus="startCalc();" onblur="stopCalc();" class="checkbox" /> Individual Webinar ($119)</label>
</tr>
</cfoutput>
Now because of the Javascript the value on all these events will be 10.00 but the NAME of the form field will be unique, and that is what I actaully want to store in the database.
This is the code I've written:
<cfparam name="seminarBulkSignUp_List" default="">
<cfoutput query="qSeminarWisTwo">
<cfparam name="FORM.#SeminarWisID#" default="">
<cfif #FORM[#SeminarWisID#]# neq "">
<cfset seminarBulkSignUp_List = ListAppend(seminarBulkSignUp_List, #FORM[#SeminarWisID#]#)>
</cfif>
</cfoutput>
<cfset FORM.SeminarWisTitle = #seminarBulkSignUp_List#>
So with this code, I run a query for ALL the possible events, and then just check against the form that has been submitted to see which ones are "blank" as in not selected, and the ones that are selected i want to add to a list to store in the database.
Now this works as far as letting me know which events were selected and which not, but i want the list to compile the actual FORM FIELD names not the value they have. How would I do that?

<cfoutput>
<cfloop list="#StructKeyList(FORM)#" index="thisField">
My field name: #thisField#<br/>
My field value: #FORM[thisField]#<br/>
</cfloop>
</cfoutput>
Apply as necessary.

Related

Coldfusion set dynamic columns for query output [duplicate]

This question already has answers here:
Dynamic Variable Naming and Reference (ColdFusion)
(2 answers)
Getting complex object error when trying to output query values
(1 answer)
Closed 6 years ago.
I am trying to set dynamic query column headers to get their values from a query.
<cfoutput query="qryGetData">
<cfloop from="-18" to="18" index="i">
<cfif i GTE 0>
<cfset variables["target_MonthPlus_#abs(i)#"] = "Testing" />
<td>
<cfoutput>#variables["target_MonthPlus_#abs(i)#"]#</cfoutput>
</td>
<cfelse>
<cfset variables["target_MonthMinus_#abs(i)#"] = "Testing" />
<td>
<cfoutput>#variables["target_MonthMinus_#abs(i)#"]#</cfoutput>
</td>
</cfif>
</cfloop>
Code I have does not really work, I found it from another answer and I have tried all I can think of and tried using EVALUATE() even though I know I should not use.
So basically my query has 37 month fields starting from target_MonthMinus18 to target_MonthMinus1. And then target_MonthPlus0 to target_MonthPlus18. I have taken care of that plus and minus with the CFIF as you can see above. So only other thing different is that value of the month.
Closest things I have got to actually name the columns dynamically is something like this, but this just outputs the name of the column, which would return target_MonthPlus0, target_MonthPlus1, targetMonthPlus2, etc.
But I need to use that name to return the actual value of the columns from the query.
<cfif i GTE 0>
<cfset monthInLoop = "target_MonthPlus_" & #ABS(i)#>
<td>
<cfoutput>#monthInLoop#</cfoutput>
target_monthMinus18 is a column name that may return a value of 100 from qryGetData that I need to display in its td
target_monthMinus17 is a column name that may return a value of 95 from qryGetData that I need to display in its td
target_monthPlus17 is a column name that may return a value of 205 from qryGetData that I need to display in its td
and so on...We are always going back 18 months in the past and 18 months in future as you can tell.
I have found several questions similar to this and have applied to my code but somehow they are trying to do different things or I still do not understand what I am doing wrong.
Thanks in advance for your help :)
PC
I think you may be over thinking this. You can access your columns directly from within the query using something like this:
<cfloop query="qryGetData">
<cfloop from="-18" to="18" index="i">
<cfif i GTE 0>
<td>
<cfoutput>#qryGetData["target_MonthPlus_" & abs(i)][currentrow]#</cfoutput>
</td>
<cfelse>
<td>
<cfoutput>#qryGetData["target_MonthMinus_" & abs(i)][currentrow]#</cfoutput>
</td>
</cfif>
</cfloop>
</cfloop>
This takes advantage of CF's query array syntax. Keep in mind you also have qryGetData.columnlist to work with (a list of all your columns). You might be able to work with that creatively as well.

How do I populate a name value inside a Coldfusion CFOUTPUT

This is an update from a previous question. I'm not sure if this is even possible but I have a CFOUTPUT tag that has a single input tag inside it. This input tag equates to 65 possible checkboxes. The problem I'm having is trying to figure out what value to put in the name attribute of the input tag. I need 22 unique names that are static and don't change. My code is as follows:
<form action="new_processOptInfo.cfm" id="displayOptions" method="post" name="displayOptions">
<cfoutput query="categorize" group="categoryName">
<h3>#UCASE(categoryName)#</h3>
<cfoutput>
<input type="checkbox" value="#idOptions#" name="option1" /> #option#<br>
</cfoutput>
</cfoutput>
<input type="submit" value="Submit" name="submitOptions" id="submitOptions" />
</form>
So how do I name the input tag?
In reply to a comment OP made.
In plain English I want to have unique names for my checkboxes that are generated automatically. I thought that when you INSERT values into a table the form tag names have to be unique
(This reply was just too many characters to leave as a comment.)
For the record, field names don't have to be unique. Cold Fusion receives duplicate field names' values in a comma delimited list. There's actually great use in that. You can have 50 checkbox named p_IDs and if 3 are checked cold fusion will recieve the values checked (like 7,15,32, if those were the values checked).
This is extremely useful with cfloops like
<cfloop list="#form.p_IDs#" index="p">Product #p# selected</cfloop>.
You can name corresponding input field, like textboxes like
<input name="desc_#dbID#" type="text">
<input type="checkbox" name="p_IDs" value="#dbID#">
And then in the cfloop on processing page use code like
<cfloop list="#form.p_IDs#" index="p">
Product #p#'s description is #form["desc_#p#"]#
</cfloop>
You could place an insert query into the cfloop (or an update, or delete query).
Examples of where this is useable is say if you wanted to mass delete selected rows, rather than deleting each row individually.
This functionality (works in a similar fashion across nearly every language) is the beauty of checkboxes. You can name them different things, but why would you want to? As far as radio buttons, naming them different things defeats their purpose.
On the subject of other input elements though, certainly name them different things.
As #FishBelowtheIce said option1 is being sent to the action page as a list so when I was made aware of that and looped through it. I just had to fix my typos and it worked. The code below is what I have now.
<cfif IsDefined("form.submitOptions")>
<cfloop index="index" list="#options#" delimiters="," >
<cfquery name="updateInsOpTable" datasource="applewood">
INSERT INTO ins_opt_table
( address,option1,option2,option3,option4,option5,option6
, option7,option8,option9,option10,option11,option12
, option13,option14,option15,option16,option17,option18
)
VALUES (#form.address#, #options#)
</cfquery>
</cfloop>
</cfif>

ColdFusion Query Output Displaying Variable Name Instead Of Field Value

I'm an "old dog" and largely self-taught on this stuff and can usually make things work (primative and convoluted as it might be), but this is the first thing that has me really stymied.
I didn't want to burden everyone with a lot of stuff to try to explain, but, here is perhaps a better explanation and example:
(#Leigh - and thank you for your time and help!) - The query is dynamic because what I desire to have is a single "universal" page combination (form page plus accompanying action page) that is used to edit multiple different (but fairly similar) record sets (so that I don't have to write a whole bunch of individual form/action page pairs).
When this "universal" "change" form page is invoked, it is passed the "ID" variable for the particular record to be edited, along with a "listID" variable unique to the particular record set containing the record to be edited.
Using the "URL.listID", the form page then looks at a pre-defined included list of record set variables (datasource, query table, field for column 1, field for column 2, etc.) pertaining to the value of the "listID" and sets (using ) the dynamic variables. Example - if "listID" is "5", which has only one column:
<cfif #URL.listID# EQ 5>
<cfset page_title = 'Change Member Role Picklist'>
<cfset datasource = '#Session.db_docs#'>
<cfset query_tbl = 'tblMemberRole'>
<cfset columns = 1>
<cfset column1_label = 'Member Role'>
<cfset column1_field = "role">
<cfset column1_input_type = "text">
<cfset column1_input_size = "100">
<cfset column1_input_maxlength = "100">
</cfif>
The query uses those variables ("ID" plus the others it got from the above list) to retrieve the individual record to be edited, and populate the "change" form.
Run query:
<cfquery name="cfqGetItem" datasource="#datasource#">
SELECT *
FROM #query_tbl#
WHERE ID = <cfqueryparam value="#URL.ID#" cfsqltype="cf_sql_integer">
</cfquery>
My "change" form (abbreviated here without table HTML) to be populated would be:
<form name="form_item_chg" action="chg_item2.cfm" method="post" enctype="multipart/form-data">
<input type="text" name="#column1_field#" maxlength="#column1_input_maxlength#" size="#column1_input_size#" value="#cfqGetItem[column1_field][currentRow]#">
<input type="Submit" value="Post Changes">
</form>
However, instead of the "change" form being populated with the VALUE for field "role", it instead is trying to use the variable name "column1_field", which it says is (true, of course) undefined in the query.
When I tried "#cfqGetItem[column1_field][currentRow]#", it says "Variable currentrow is not defined".
When I tried "#cfqGetItem.column1_field#", it says "Element column1_field is not defined in query cfqGetItem".
I apologize in advance for not knowing/using all the correct terminology, and hope I am explaining this reasonably clearly. I suspect I will have to revert to writing individual form-page/action-page pairs. Thank you to all for your time and help!
ORIGINAL POST:
I'm not highly technical, and this is probably something simple, but here is
my dilemma, where I am attempting to retrieve a single record using a variable name in the query.
First, I define some variables:
<cfset ID = #Form.ID#<!--- the single record I want to retrieve, passed from a form --->
<cfset datasource = 'MyDatabase'>
<cfset query_tbl = 'MyDatabaseTable'>
<cfset field1 = 'actual_fieldname1'><!--- field in MyDatabaseTable --->
<cfset field2 = 'actual_fieldname2'><!--- field in MyDatabaseTable --->
ETC.
Then, to retrieve this single record, I run a query using those variables:
<cfquery name="cfqGetItem" datasource="#datasource#">
SELECT *
FROM #query_tbl#
WHERE ID = #ID#
</cfquery>
Then, I attempt to display the query output:
EITHER AS
<cfoutput>
<p>#cfqGetItem.field1#
<p>#cfqGetItem.field2#
</cfoutput>
OR, AS
<cfoutput>
<p>#cfqGetItem[field1][currentRow]#
<p>#cfqGetItem[field2][currentRow]#
</cfoutput>
In each case, I get a similar CF error message: "Element field1 is not defined in query cfqGetItem", or "Variable currentrow is not defined".
How can I get the query output to generate the actual values for the record instead of the variable names?
Thank you very much for any help!
If you change this:
<cfoutput>
<p>#cfqGetItem[field1][currentRow]#
<p>#cfqGetItem[field2][currentRow]#
</cfoutput>
to this:
<cfoutput>
<cfloop query="cfqGetItem">
<p>#cfqGetItem[field1][currentRow]#
<p>#cfqGetItem[field2][currentRow]#
</cfloop>
</cfoutput>
You should be ok. However, this is the simplest way
<cfoutput query="cfqGetItem">
<p>#actual_fieldname1#
<p>#actual_fieldname1#
</cfoutput>
Setting the table and field names to variables complicates matters. Unless they really can vary, don't do it. Also, the cfqueryparam tag is your friend. Use it in places like this:
where id = <cfqueryparam
cfsqltype = "cf_sql_integer"
value = "#id#">
You can try out like this also:
<cfquery
name="GetParks" datasource="cfdocexamples"
>
SELECT PARKNAME, REGION, STATE
FROM Parks
ORDER BY ParkName, State
</cfquery>
<cfoutput>
<p>#GetParks.PARKNAME#
<p>#GetParks.REGION#
</cfoutput>
<cfoutput >
#GetParks['state'][GetParks.RecordCount]#
</cfoutput>
<cfoutput >
#GetParks['state'][2]#
</cfoutput>

cfquery ColdFusion execution

Does cfquery execute on every page load? I ask because I'm getting a sequence number with the query and then using it in a form. Unfortunatly, the query seems to execute every time the page loads. I don't want that to happen. I also tried putting it inside of a cffunction and then calling it out of the onSubmit parameter of of the cfinput box that uses the sequence number, but it still calls the sequence.
Here are examples of the way I've tried to do this:
<cfquery name="payment_seq_num" datasource="ORCL">
select ratner01.payment_id_seq.nextval as seq from dual
</cfquery>
<cfset paymentid = payment_seq_num.seq>
And
<cffunction name="getVetSeq" output="false">
<cfquery name="vet_seq_num" datasource="ORCL">
select ratner01.vet_id_seq.nextval as seq from dual
</cfquery>
<cfset vet_form.VET_ID = vet_seq_num.seq>
</cffunction>
I get why the first one keeps incrementing...it's in the head and is called everytime. But why would the second one execute every page load?
Here's how I'm calling it:
<cfform action="vet_output.cfm" method="post" format="html" class="cfform" name="vet_form">
<fieldset>
<legend>Add a Veterinarian to the Databse</legend>
<table>
<tr><cfoutput>
<td><cfinput type="hidden" name="VET_ID" onsubmit="#getVetSeq()#"></td></cfoutput>
</tr>
<tr>
<td>Vet First Name:<br/> <cfinput type="text" name="VET_FNAME" maxlength="35"></td>
<td>Vet Last name: <br/><cfinput type="text" name="VET_LNAME" maxlength="50"></td>
</tr>
<td><cfinput type="submit" value="Insert" name="vetSubmit"></td>
</table>
</fieldset>
</cfform>
So I added this into the output page and removed all related code from the input page, thanks to some suggestions, and it worked... :
<cfquery name="vet_seq_num" datasource="ORCL">
select ratner01.vet_id_seq.nextval as seq from dual
</cfquery>
<cfset FORM.VET_ID = vet_seq_num.seq>
<cfinsert name="insert_vet" datasource="ORCL" username="XX" password="XX"
tablename="VET"
formfields="VET_ID, VET_FNAME, VET_LNAME">
Yeah, so every time this page is loaded you will call that function and get a new sequence number. Because everytime you load the page #getVetSeq()# will be executed by ColdFusion.
I know you put it in onSubmit() but onSubmit() is a JavaScript event, which has no knowledge of ColdFusion. By the time JavaScript sees that code the function has already been called. If you look you'll probably see JS errors because when you click submit you are actually calling a non-existant function. Because your code renders as something like:
onsubmit="1234"
If you only want it called when the form is submitted then do it in your output.cfm instead of in your form.
If for some reason you need to do it on this page instead of in your processing page, then you'll need to look at doing it as an Ajax call so that it only executes onSubmit().
<cfset vet_form.VET_ID = vet_seq_num.seq>
Form values submitted via method="POST" are available in a system structure named FORM. It is always called FORM, regardless of the name assigned to your <form>. So the correct variable name is:
FORM.VET_ID
However, it is cleaner not to access the FORM scope from within the function. Just have the function generate and return the new ID and leave the rest up to the calling page. That makes the function more modular/resuable. But remember to var scope all function local variables (for ColdFusion 9+ use the Local scope)
<!--- Usage --->
<cfset FORM.VET_ID = generateNewVetID()>
<!--- Function --->
<cffunction name="generateNewVetID" output="false" returnType="numeric">
<cfset var vet_seq_num = "">
<cfquery name="vet_seq_num" datasource="ORCL">
SELECT ratner01.vet_id_seq.nextval AS seq
FROM dual
</cfquery>
<cfreturn vet_seq_num.seq>
</cffunction>

Double evaluation - How do I access my query column based on a variable that holds the column name?

I have a query and a list of field/column names. I want to do a sort of double loop - loop through each record in the query, and then loop through the list of field/column names and output each corresponding field. The loops should be something like this:
<table>
<cfoutput query="myQuery">
<tr>
<cfloop list="#cols#" index="col">
<td>?</td>
</cfloop>
</tr>
</cfoutput>
</table>
The problem is what to put where the question mark is... I've tried #myquery[col]#, but this didn't work. I need to get the variable indicated by the string name in the variable col... And obviously, #col# will just return the column name. I need to figure out some way to double-evaluate the string... something like ##col##, which of course won't work either. How can I accomplish this?
When referencing column names as a structure, you need to also tell the query which row you want to get. You should also make sure that you check that the column name exists if you didn't get the cols variable via myQuery.ColumnList.
Use the following code to dynamically reference each column in your loop:
<table>
<cfoutput query="myQuery">
<tr>
<cfloop list="#cols#" index="col">
<td>#myQuery[col][CurrentRow]#</td>
</cfloop>
</tr>
</cfoutput>
</table>
You can still use Sergii's approach with your own columnlist:
<cfloop list="#cols#" index="col">
<cfif StructKeyExists(myQuery, col)>
<td>#col# = #myQuery[col][myQuery.CurrentRow]#</td>
</cfif>
</cfloop>
Got it!! :)
#evaluate(evaluate("col"))#