How to output variables dynamically in ColdFusion - dynamic

I am trying to replace a value with a passed value using javascript or coldfusion. The idea is we have a template which outputs values in the grid. But the values displayed need be dynamically determined by a query. So far I haven't been able to come up with any good ideas.
Here is a sample of something I thought would of work (but does not). It may explain what I am trying to do:
<cfset StaticValue="DynamicValue">
<cfset DynamicValue="What I Want To Show">
<script type="text/javascript">
document.getElementById("demo").innerHTML="<cfoutput>#StaticValue#</cfoutput>";
</script>
<cfoutput>#<span id="demo"></span>#</cfoutput>

To use an existing string/variable as a variable name, you need to use bracket notation.
In CF, if you haven't explicitly scoped a variable, it is created in the Variables scope, so you can do:
<cfset StaticValue="DynamicValue">
<cfset DynamicValue="What I Want To Show">
<!--- outputs value of Variables.DynamicValue --->
<cfoutput>#Variables[StaticValue]#</cfoutupt>
(This works will all scopes/structs/queries/etc)
NOTE: If the variable is to be output inside of a JavaScript string, you need to wrap it in JsStringFormat(...) to ensure the appropriate characters are escaped.

First of all, this is wrong:
<cfoutput>#<span id="demo"></span>#</cfoutput>
You are suggesting that there is a CF variable named
<span id="demo"></span>
Your question isn't clear, so neither can be my answer, but I suspect that you are trying to do this:
<script type="text/javascript">
<cfoutput>
document.getElementById("demo").innerHTML="#StaticValue#";
</cfoutput>
</script>
<span id="demo"></span>
When this JavaScript runs on the page, the string "DynamicValue" will be written into the span#demo.

Are you trying to compare if they are the same and then display it if they are?
<cfset StaticValue="DynamicValue">
<cfif staticvalue is dynamicvalue>
<cfset DynamicValue="What I Want To Show">
<script type="text/javascript">
<cfoutput>
document.getElementById("demo").innerHTML="#DynamicValue#";
</cfoutput>
</script>
</cfif>
Not sure what u are trying to do here - as this will error on bad variable inside your # #
<cfoutput>#<span id="demo"></span>#</cfoutput>
Should be:
<cfoutput>
<span id="demo">
#dynamicvalue#
</span>
</cfoutput>
If you are just trying to get a url to display:
<cfoutput>
<span id="demo">
#dynamicvalue#
</span>
</cfoutput>

Related

cfml variable inside cfoutput text

I'd like to resolve this problem. I have a variable:
<cfset myvar = "mytext <cfinclude template=""test.cfm"">">
and I have test.cfm with an amount of cfml code.
and I'm trying to make cfoutput like this:
<cfoutput>#myvar#</cfoutput>
I'd like that my page doesn't show this output:
mytext <cfinclude template="test.cfm">
but:
mytext followed by the execution of cfml code inside test.cfm
Is it possible?
I am not certain what you are trying to do from your question but if my guess is correct this may do what you want:
<cfsavecontent variable="myvar"><cfoutput>
mytext
<cfinclude template="test.cfm">
</cfoutput></cfsavecontent>
<cfoutput>
#myvar#
</cfoutput>
Note: You may not need the cfoutput inside the cfcontent tag, it depends on your page setup so I have just added it.
I have no idea what your included test.cfm file is doing since you did not share that information but this might work for you:
<cfset myvar = "mytext ">
<cfoutput>#myvar#</cfoutput>
<cfinclude template="test.cfm">
Assuming that the included file generates the output that you are looking for.
In your code, you are trying to set the cfinclude tag itself to another variable. This will never work. However, I have tested the following on a live server and it does work.
<cfsavecontent variable="inc">
<cfinclude template="test.cfm">
</cfsavecontent>
<cfoutput>mytext #inc#</cfoutput>
In this example, everything between the cfsavecontent tags is saved to a variable, in this case inc. Then you are able to reference that variable in the cfoutput tag.
Or, to use <cfset> as in your code:
<cfsavecontent variable="inc">
<cfinclude template="test.cfm">
</cfsavecontent>
<cfset myvar = "mytext" & inc>
& is the CFML string concatenation operator.
It's difficult to know for sure without seeing more of your code, but if you are trying to use includes in this way your code is perhaps disorganized. Providing a full example would help provide some context.

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>

Excess Characters Being Generated by ColdFusion Query/output

I've got a strange issue with some Coldfusion/SQL Query output. The actual data is generating properly, but at the bottom of the page it's outputting "Library/Library/Library/Library/Library/Library/Library/Library" for no reason I can discern.
It always outputs it in exactly that format, always 8 times, no matter how many terms I'm searching for, how many records are returned, or how much text is generated after grouping.
It doesn't happen on all pages, but it does seem to happen on every page on the site that pulls a query from this database...
I'm lost. Code below, live page is here: http://www.audiogo-library.com/client/client_pages/hachettepage.cfm
<cfsetting enablecfoutputonly="yes" showdebugoutput="no">
<!--- Custom Hachette page --->
<cfset todayDate = Now()>
<!--- Link to Style Sheets --->
<img style="margin:auto" src="http://www.audiogo-library.com/Library/client/client_images/hachettelogo.gif"></br>
<cfoutput> #MonthAsString(Month(Now()))# </cfoutput> Releases</br></br>
<cfquery name="GetProductBasicInfo" datasource="#Request.Application.PowerWeb.datasource#" dbtype="odbc">
SELECT product.ProductID, productmarket.imprint, product.IsbnUpc, product.Title, product.FullTitle, product.SubTitle, product.PubDate, product.SKU, productmarket.descriptionshort, productmarket.productform, productmarket.NoOfPieces, productmarket.productmarketid
FROM Product, ProductMarket
WHERE product.productid = productmarket.productid AND product.IsbnUpc LIKE '%61113%' AND product.PubDate BETWEEN '<cfoutput>#DatePart("m", todayDate)#</cfoutput>/01/<cfoutput>#DatePart("yyyy", todayDate)#</cfoutput>' AND '<cfoutput>#DatePart("m", todayDate)#</cfoutput>/31/<cfoutput>#DatePart("yyyy", todayDate)#</cfoutput>'
ORDER BY product.FullTitle ASC
</cfquery>
<cfoutput query="GetProductBasicInfo" Group="FullTitle">
<table width="90%" border="0" style="margin-top:15px;">
<tr>
<td><p><a href="http://www.audiogo-library.com/library/productdetails.cfm?sku=#SKU#">
<cfif #FullTitle# eq ''> <div class="title"> #Title# </div>
<cfelse> <div class="title">#FullTitle# </div> </a>
</cfif></p>
<p>
<cfif #descriptionshort# neq ''> #descriptionshort# </cfif>
</p>
</td>
<td width="30%"> <img src="http://www.audiogo-library.com/library/client/Products/ProdimageLg/#SKU#.jpg"></td>
</tr>
</table>
</cfoutput>
TestText
I actually solved it by accident while trying to push the "/Library"s down the page. It turns out the cfsettings tag built into the query/output was disabling non cfoutput content. THe guy who built the footer was relying on inheriting "enablecfoutputonly='false'", and this code changed that. Everything from the site's footer was being hidden except for the section of each address that was generated by cfoutput. SO yeah, if anyone else has this or a similar problem, check your cfsettings tag, and ensure you DISABLE
Go to cfadmin and disable query caching. Restart the CF service. Voila!...no more extra data.

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

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.

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>