Problem with the value portion of a drop down menu in ColdFusion - sql

Here is what I have inside of my select statement:
<cfloop query name="country">
<option value="#CountryName#"><cfoutput>#CountryName#</cfoutput></option>
</cfloop>
Everything works fine, but the value it passes to my URL filter is #CountryName#, not the actual country name (i.e. Canada).
How can I assign it the value of the country name and not the variable name?

The #CountryName# variable is not being evaluated because it is outside your output tags. Move the tags so they encompass your value too.
<cfoutput><option value="#CountryName#">#CountryName#</option></cfoutput>
Or just use a <cfoutput query="..."> instead of <cfloop>.

Related

Determining select from select multiple

This may have a simple answer but I could not find an answer by searching. I am using Selenium with Java.
I have several elements (WebElement ele). I locate them by means other than their direct tag names so I cannot simply use the xpath to answer the question I have.
I have several possible element types:
<div>
<input>
<input type = "checkbox">
<select>
<select multiple>
I can determine most of them. If I do ele.getTagName(). If it is a div I will know right away. If it is input I can do a ele.getAttribute("type") and see whether it is checkbox or not. But for select the tagname will give me select both for the regular select and the select multiple. There is no attribute name for the multiple, so how can I distinguish between select and select multiple ?
You could do something like this:
//if its a select element
Select se = new Select(ele);
Then you could simple check if this is a multi select element by running:
se.isMultiple()
You can use XPath expression to choose select with multiple attribute:
//select[#multiple]
or select without multiple attribute:
//select[not(#multiple)]
Also note that even without explicit value of multiple attribute, actual value is "true", so if multiple attribute present in select, ele.getAttribute("multiple") should return "true"

cfselect has duplicate options when pulling a record into a form

I am trying to get the form on my edit screen to pull what is in the record selected, and add the options that are still available that dont include what is selected. So if the current record has a ROC_Group of 3, the dropdown has 3 selected, and 4 and 5 as options. This is as close as I can get it, but end up with what is currently in the record (3), and 3,4 and 5 as options. So it looks like there is a duplicate in the dropdown. Any suggestions? Thank you
<cfselect name="ROC_GROUP" ><cfoutput query="GetSiteNotoUpdate">
<cfif GetSiteNotoUpdate.ROC_GROUP is "#ROC_GROUP#">
<option value="#ROC_GROUP#" selected="yes">#ROC_GROUP#</option>
<option>3</option><option>4</option><option>5</option>
<cfelse>
<option value="#ROC_GROUP#">#ROC_GROUP#</option>
</cfif>
</cfoutput></cfselect>
<cfif GetSiteNotoUpdate.ROC_GROUP is "#ROC_GROUP#">
Your comparison is using two variables, both named ROC_GROUP. If the second one refers to a different variable than GetSiteNotoUpdate.ROC_GROUP, you need to scope it. Otherwise, CF will not know which one you mean and will likely interpret the above as <cfif someQueryVariable equals itself>, which is always true.
Simply put the "currently selected" value into a separate variable, like CurrentlySelectedGroup. Then compare it with each value in the query inside your loop:
<select name="ROC_GROUP">
<cfoutput query="GetSiteNotoUpdate">
<option value="#ROC_GROUP#"
<cfif GetSiteNotoUpdate.ROC_GROUP eq CurrentlySelectedGroup>selected="yes"</cfif>>
#ROC_GROUP#
</option>
</cfoutput>
</select>
As an aside, since you are not using any of the extra features, there is no need to use <cfselect>. Just use a plain html <select>.

how to get a value out of a ColdFusion struct object

I am creating a pdf document (via ColdFusion), and rendering the pdf in a browser. The pdf form is already created and I am prefilling and populating the fields.
So what I am doing is dumping the variables out from the pdf to use as the name in a cfpdfformparam. (to get the variables) Then what I am doing is creating where owner email is the name of the variable from the pdf and then for the value I am assigning the session variable from another form. So that what they have entered prefills in the form they need to fill out. So really name decides the location on the pdf and value is the session variable of what was entered on the other form.
The issue I am having is that the variables that were already created, a few of them have structs inside of the variables so I am not sure how to call them in (name portion) in order to prefill and populate the information.
For example how would I prefill in the information for FEID/DL/DMVacct and for FL reg when they contain a struct within the variable?:
like this?: <cfpdfformparam
name="FEID/DL/DMVacct##"
value="#session.checkout.info.driverlicense_1#">
<cfpdfform action="read" source="82040y.pdf" result="data" />
<cfdump var="#data#" />
I am pre-populating the pdf form fields (via ColdFusion session variables), and then rendering the pdf using the following markup:
<cfpdfform source="82040.pdf" action="populate">
<cfpdfformparam name="org" value="">
</cfpdfform>
Any help would be greatly appreciated!
You can access variables embedded inside a struct like this:
<!--- using dot notation --->
<cfif StructKeyExists(myStruct, "myKey")>
<cfoutput> #mystruct.myKey#</cfoutput><br>
</cfif>
<!--- or using access notation --->
<cfif StructKeyExists(myStruct, LastName)>
<cfoutput>#LastName#: #mystruct[LastName]#</cfoutput><br>
</cfif>
You can use IsDefined to see if a value exists:
IsDefined("structure_name.key")>
However, if the key is dynamic, or contains special characters, you must use the StructKeyExists function.
NOTE: You must be careful about your variable names in ColdFusion. Some of the names assigned to your struct values would be considered invalid if used as variable names. So if your struct names contain invalid characters, you will need to access them via access-notation in order to retrieve them:
<!--- use access-notation for value names with special chars --->
data["FEID/DL/DMVacct"]["#"]
Otherwise, you will have runtime errors if you attempt dot-notation:
<!--- Invalid markup! Don't do this! --->
#data.FEID/DL/DMVacct.##
So here are some examples of how you could access your data:
<!--- Output the value --->
<cfoutput> #data["FEID/DL/DMVacct"]["#"]#</cfoutput><br>
<!--- Storing the value in a variable named 'myVar' --->
<cfset myVar = #data["FEID/DL/DMVacct"]["#"]# />
<cfoutput>#myVar#</cfoutput>
If your struct names follow ColdFusion's variable naming rules, then you can also access your data with dot-notation, notice I changed the value names FEID/DL/DMVacct and # to valid variable names: FEID_DL_DMVacct and num:
<!--- Output the value via dot-notation --->
<cfoutput> #data.FEID_DL_DMVacct.num#</cfoutput><br>
<!--- Storing the value in a variable named 'myVar' --->
<cfset myVar = #data.FEID_DL_DMVacct.num# />
<cfoutput>#myVar#</cfoutput>
Hope this helps!
structName['FEID/DL/DMVacct']['##'] = variable
You need the double # to escape them and produce one singular #

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>