ColdFusion not able to read option value of a cfselect - coldfusion

I'm a beginner with ColdFusion and I'm just trying out some basic functions.. I tried to loop over a simple query and put the values in a of a element. As value for the element I tried to set the id of each record of the query. After submiting I tried to read the selected value but I only get
You have chosen #getAll.id#
Here is my Code:
index.cfm
<cfquery datasource="testdb" name="getAll">
select *
from Personen
</cfquery>
<cfform action="chosen.cfm" method="post">
<cfselect name="listPersons">
<cfloop query="getAll">
<option value="#getAll.id#"><cfoutput>#getAll.id# #getAll.name# #getAll.vorname# #getAll.gebdate# <BR></cfoutput>
</cfloop>
</cfselect>
<cfinput type="Submit" name="Senden" value="Senden">
</cfform>
chosen.cfm
<cfoutput>You have chosen #listPersons#</cfoutput>
Can you tell me where I've made the mistake?

You didn't put your value attribute in a cfoutput tag, so it's being processed as #getAll.id# as the key in the struct instead of the value from the query. If you update your cfloop to be a cfoutput your issue will be fixed.
A couple pointers - You should scope the variable on chosen.cfm and you don't need to use cfform a regular form works just fine.
<cfquery datasource="testdb" name="getAll">
select *
from Personen
</cfquery>
<form action="chosen.cfm" method="post">
<select name="listPersons">
<cfoutput query="getAll">
<option value="#getAll.id#">#getAll.id# #getAll.name# #getAll.vorname# #getAll.gebdate#</option>
</cfoutput>
</select>
<input type="Submit" name="Senden" value="Senden">
</form>
chosen.cfm
<cfoutput>You have chosen #form.listPersons#</cfoutput>

Your code works for me with my test database but the value of listPersons on Chosen.cfm is not what I think you intended it to be. I would change the code to the following:
<cfquery datasource="testdb" name="getAll">
select *
from Personen
</cfquery>
<cfform action="chosen.cfm" method="post">
<cfselect name="listPersons">
<cfoutput query="getAll">
<option value="#getAll.id#">#getAll.id# #HTMLEditFormat(getAll.name)# #HTMLEditFormat(getAll.vorname)# #getAll.gebdate#
</cfoutput>
</cfselect>
<cfinput type="Submit" name="Senden" value="Senden">
</cfform>
What I did is I changed your CFLOOP to a CFOUTPUT then removed the CFOUTPUT you had. I also added the HTMLEditFormat functions just in case NAME or VORNAME contain some characters that will not play nice with the display. I assumed ID is numeric and GEBDATE is a date so figured no need on those. I also removed the BR element from your OPTION, not that I thought it was causing an issue but I could not see how that would effect the display either so seemed unneeded. I'd personally would close the OPTION but it is not needed to run. If you ultimate code is not running anything that CFFORM offers then I'd not use it and just use an HTML FORM.
Then on Chosen.cfm I would scope the output:
<cfoutput>#Form.listPersons#</cfoutput>

<cfoutput query="getAll">
#id# #name#
</cfoutput>
You don't need to repeat the query name inside of a cfoutput loop, if with cfoutput you specify the query you are looping over.

Related

Creating a structure as a button in ColdFusion

First off, I'm not really sure how to title this question so I apologize if it's vague. I am trying to create a shopping list using ColdFusion and I've ran into a bit of a snag. I want a delete button to appear next to the item that's been created. I have almost everything working, but I don't understand structures enough in ColdFusion to understand what I am doing wrong. Is it similar to a component in React.js? I ran into an issue saying that the variable "button" is not defined. I'm assuming this is because structkeyExists can't identify a single button. Why would this work for form and not for a button?
Here is my code:
<cfif structKeyExists(form, "submitButt")>
<cfquery datasource="ESC-ADD-TECH">
INSERT INTO Main(itemDesc) VALUES('#itemDesc#')
</cfquery>
</cfif>
<cfif structKeyExists(button, "delete_butt")>
<cfquery datasource="ESC-ADD-TECH">
INSERT INTO Main(itemDesc) VALUES('#itemDesc#')
</cfquery>
</cfif>
<cfquery datasource="ESC-ADD-TECH" name="items">
DELETE FROM Main
WHERE itemDesc = '#itemDesc#'
</cfquery>
<body>
<div id="myDIV" class="header">
<h2>My Shopping List</h2>
<form method="POST">
<input type="text" name="itemDesc" placeholder="Title...">
<input name="submitButt" type="submit" class="addBtn">
</form>
</div>
<cfoutput query="items">
<li>#items.itemDesc# <button class="delete" name="delete_butt">x</button></li>
</cfoutput>
</body>
Is there a way to do what I am trying to do here using a structure? Am I better off creating the button in javascript and try to create a structure as a boolean statement and just have javascript rewrite that value? Kinda just shooting in the dark here, but I would appreciate any and all help.
Thank you everyone!
So there isn't going to be a "button" structure from your form being submitted.
The first thing to remember is that a ColdFusion structure is just a collection of key/value pairs (similar to a JavaScript object), and unless the value is set, will be undefined.
In your case, the "form" struct exists because you are submitting your page back to itself with your input[type="submit"]. Which for a ColdFusion page, will create a form struct with keys for each named input within the submitted form, the values of which are pulled from those elements' value attributes.
If you are trying to use the form struct to handle deleting items, you may be better served using radio buttons/checkboxes to select which item(s) to delete, and set the action to take using the value attribute of your submit buttons.
Using your code as an example:
<cfparam name="form.action" type="string" default="none">
<cfswitch expression="#form.action#">
<cfcase value="insert">
<!---Your insert query goes here--->
</cfcase>
<cfcase value="delete">
<!---Your delete query goes here--->
</cfcase>
<cfdefaultcase></cfdefaultcase>
</cfswitch>
<!---Your select query--->
<body>
<form method="post" action="#">
<div id="myDIV" class="header">
<h2>My Shopping List</h2>
<input type="text" name="itemDesc" placeholder="Title...">
<button type="submit" name="action" value="insert">Submit</button>
</div>
<ul>
<cfoutput query="items">
<li>#items.itemDesc#
<input type="radio" name="delDesc" value="#items.itemDesc#"/>
</li>
</cfoutput>
</ul>
<button type="submit" name="action" value="delete">Delete</button>
</form>
</body>
In this case you will use form.itemDesc when inserting values, and form.delDesc when deleting items.

cfloop and dynamic variables from database

I have a small form that loops from 1 to 30 and assigns variables. After these variables are saved to the database, I am having some trouble figuring out how to set the value of the input as the value from the database. Here is a some of my code.
<cfloop index="i"
from="1"
to="30">
<select name="supply_#i#"
id="supply_#i#"
/>
<cfloop query="GetDescriptor">
<option value="#GetDescriptor.Code#">
#GetDescriptor.Descriptor#
</option>
</cfloop>
</select>
<input type="text"
name="quant_#i#"
id="quant_#i#"
/>
</cfloop>
Once complete, the user saves all 30 values to the database. When they attempt to view or edit the form, I need to show the previously selected value. I really don't know how to "nest" coldfusion variables together, or if it is even possible, but here is an example of the VIEW/EDIT .cfm that does not work, but shows you what I am trying to accomplish with the values.
<cfloop index="i"
from="1"
to="30">
<cfset "supply_#i#" = #QUERY.supply#i##>
<cfset "quant_#i#" = #QUERY.quant#i##>
<select name="supply_#i#"
id="supply_#i#"
/>
<cfloop query="GetDescriptor">
<option value="#GetDescriptor.Code#"
<cfif #VARIABLES.supply_#i## EQ '#GetDescriptor.Code#'>
SELECTED="SELECTED"
</cfif> >
#GetDescriptor.Descriptor#
</option>
</cfloop>
</select>
<input type="text"
name="quant_#i#"
id="quant_#i#"
value ="#VARIABLES.quant_#i##"
/>
</cfloop>
I think the easiest way to accomplish this is to use array notation. That is, use brackets instead of dots. Since all variables scopes are structs you can do this.
variables["foo"]
is equivalent to
variables.foo
So in your case, you probably want to reference
#VARIABLES["supply_#i#"]#
or, perhaps
#VARIABLES["supply_" & i]#
(Too long for comments)
What is the actual structure of the db table the values are inserted into? The variable names suggest you are using a denormalized db design. If so, consider normalizing the structure and storing the data in rows (SupplyCode, Quantity), rather than columns (Supply1,Qty1,Supply2,Qty2,....). The normalized structure is a lot easier to query, and supports as many or as few entries as needed.
Simply query the table and use a query loop to display the existing values. Use currentRow in place of the index variable i:
<cfquery name="existingEntries" ...>
SELECT SupplyCode, Quantity
FROM YourTable
ORDER BY .....
</cfquery>
<cfloop query="existingEntries">
<select name="SupplyCode_#currentRow#" id="SupplyCode_#currentRow#">
<cfloop query="GetDescriptor">
<option value="#GetDescriptor.Code#"
<cfif existingEntries.SupplyCode EQ GetDescriptor.Code>
SELECTED="SELECTED"
</cfif>>
#GetDescriptor.Descriptor#
</option>
</cfloop>
</select>
<input type="text" name="Quantity_#currentRow#" value ="#existingEntries.Quantity#" .... />
</cfloop>
NB: Typically you would store a FK ID from the GetDescriptor table, ie "SupplyID" rather than a string code or description, ie "SupplyCode"

ColdFusion form - display error details from database table

I have a database table with error id and error details for form validation errors.
In my ColdFusion form page, I have a list of error numbers 2,4,7 available. But I want to display errors like "Please enter name" etc.
I want to compare my list of errors with error id's in the database table and display the corresponding error in my form. Please let me know if there is any better way to do it. Thanks in advance!!
<form name = "empform" action="empform.cfm" method="post">
<cfoutput>
<table border="0">
<tr><td colspan="4" align="center" style="padding-bottom:20px;"><h2>Add Employee</h2></td></tr>
<cfif (structKeyExists(rc,"addemp"))>
<cfif rc.result.hasErrors()>
<tr>
<td colspan="4" style="border:2px solid red;">
Please review the following:
<ul>
<cfloop array="#rc.result.getFailureMessages()#" index="message">
<li>#message#</li>
</cfloop>
</ul>
</td>
</tr>
</cfif>
</cfif>
<cfset myArray = rc.result.getFailureMessages()>
<cfset myList = ArrayToList(myArray, ",")>
<cfquery name="qErrorMessages" datasource="#dsn#" >
Select * from ErrorMessages
</cfquery>
<tr>
<td><label><b>Emp Last Name</b></label></td>
<td><input name="lastname" type="text" value="" /></td>
<td><label><b>First Name</b></label></td>
<td><input name="FirstName" type="text" value="" /></td>
</tr>
<tr>
<td><label><b> Emp Birth Date</b></label></td>
<td><input name="birthdate" type="text" value="" /></td>
<td><label><b>Salary</b></label></td>
<td><input name="salary" type="text" value="" /></td>
</tr>
<tr><td style="padding-top:10px;">
<input type="submit" name = "addemp" value ="AddEmp /></td>
</tr>
</table>
</cfoutput>
</form>
I can display errors as shown above
Now I want to show actual messages. I am stuck up here.
My issue is not solved. I am able to loop through the table of errors only. Probably I need to loop through my list of error ids and then I need another loop to look up for the table errorId's to match then display the error.
I got it fixed with the below code.
<cfloop index="ind" list=#mylist#>
<cfquery datasource= "#dsn#" name="emperrors">
Select errorid,errmessage from errorcodes where errorid = #ind#
</cfquery>
#emperrors.errmessage#<br>
</cfloop>
Your question is short on details, but I'd probably query the table with the error messages, convert it to a struct, then drop that struct into a persistent scope that I could then reference later.
Something like:
<cfset errorStruct={}>
<cfloop query="qFormErrorMsgs">
<cfset errorStruct[error_id]=error_msg>
</cfloop>
<cfset application.errorStruct=errorStruct>
Then, when you want to display the error:
<p>Please fix the following error: #application.errorStruct[variables.error_id]#</p>
But that's just one way you might do it. Without more information in your question all you're going to get is guess (if you're lucky).
Alright, your answer detailing code should have been appended to your question via an edit, and still that's really not enough code. How are you determining your errors?
It sounds like your errors are something like this:
Please review the following:
2
3
6.
When what you want is error messages.
An easy way to update this is to load all your messages into an array where the index corresponds to the error codes.
<cfset ErrDetails=ArrayNew()>
<cfset ErrDetails[1]="You didn't enter your first name.">
<cfset ErrDetails[2]="You didn't enter your last name.">
<cfset ErrDetails[3]="You didn't enter a properly formatted birth date.">
<cfset ErrDetails[4]="It appears, from your birthdate, that your age is below 18">
And then, in your cfloop, you can
<cfloop array="#rc.result.getFailureMessages()#" index="message">
<li>#ErrDetails[message]#</li>
</cfloop>
Lastly it sounds like your errorcodes/messages are coming from a database. I'm not really sure that's necessary, but if you really want to keep it that way, that's fine. You can initialize this into the application scope so that you can call it later without rerunning the query, or just put it somewhere in flat text. However, if you want the application scope answer, here's a method.
<cflock scope="application" timeout="5">
<cfif not isDefined("application.ErrDetails")>
<cfquery name="getec">
select ErrID,ErrMessage from ErrorCodes
</cfquery>
<cfset Application.ErrDetails = []>
<cfloop query="getec">
<cfset Application.ErrDetails[ErrID]=ErrMessage>
</cfloop>
</cfif>
<cfset request.ErrDetails = Application.ErrDetails>
</cflock>
Once this is initiated, the cfloop displaying errors as I showed above can use #request.errDetails[message]#
If it's just running in one page, I wouldn't worry about creating an application variable. I'd just hardcode it in, or do a query there with a cfloop similar to above writing to a variables scope variable, not an application variable so the cflock isn't needed.
Unless you also have web based admin creating these rules, I'd probably ditch the table altogether and drop it directly into code, a cfinclude, a udf, or a custom tag.
Since the asker has updated their question with an answer.
Your answer needlessly repeats the query for each iteration.
Try something like this..
<cfquery datasource="#dsn#" name="GetECs">
select ErrorID,ErrMessage from ErrorCodes
where ErrorID in (<cfqueryparam cfsqltype="cf_sql_integer" list="yes" value="#mylist#">)
</cfquery>
<cfoutput query="GetECs">
#currentrow#. #ErrMessage#<br>
</cfoutput>
Notes:
One query is run.
For results, the where ErrorID in (<cfqueryparam cfsqltype="cf_sql_integer" list="yes" value="#mylist#">) is the same as saying where ErrorID in (#mylist#). However, the tag secures your queries against sql injection (the tactic of modifying variables to produce undesirable/malicious results in a query. Read up on cfqueryparam at http://help.adobe.com/livedocs/coldfusion/8/htmldocs/help.html?content=Tags_p-q_18.html

How to display each element in a ColdFusion query

I have written this piece of CF code to get and display the data from database. (Actually to populate text fields.) The problem: I am not getting values from the query, but I am correctly getting the number of records.
How am I supposed to access the values returned by the query via a cfloop? Below is my work.
<cfquery name="data_query" datasource="#dsn#">
SELECT
id,
name
FROM learning
</cfquery>
<cfloop query=data_query">
<li>
<div class="list_div clearfix">
<input type="text" value="#URLDecode(name)#">
</div>
</li>
</cfloop>
</cfquery>
You have two options:
Wrap the vars output line with <cfoutput /> tags:
<cfoutput>#id#: <input type="text" value="#name#"></cfoutput>
Use <cfoutput query="data_query"> loop instead of <cfloop ...>
For the sake of cleaner code I would prefer the second option so your code would be:
<cfquery name="data_query" datasource="#dsn#">
SELECT
id,
name
FROM learning
</cfquery>
<cfoutput query="data_query">
<li>
<div class="list_div clearfix">
#id#: <input type="text" value="#name#">
</div>
</li>
</cfoutput>
Also you should properly 'scope' your query columns when outputting. This will make your code easier to maintain in future, e.g. you'll always know that #data_query.name# belonged to the query and wasn't some string set by some other piece of code somewhere. And it'll speed up page performance - if you don't scope variables (this applies to all types of variables, not just queries), then CF will loop through the different scopes until it finds something with this value. So by scoping, you prevent CF having to loop.
<cfquery name="data_query" datasource="#variables.dsn#">
SELECT
id,
name
FROM learning
</cfquery>
<cfoutput query="data_query">
<li>
<div class="list_div clearfix">
#data_query.id#: <input type="text" value="#data_query.name#">
</div>
</li>
</cfoutput>
On the whole your logic was fine.. just a few typos and minor changes needed..
Give this a try.
<cfquery name="data_query" datasource="#dsn#">
SELECT
id,
name
FROM learning
</cfquery>
<cfloop query="data_query">
<li>
<div class="list_div clearfix">
#id#: <input type="text" value="#name#">
</div>
</li>
</cfloop>
And if you didn't know about it:
<cfdump var="#data_query#">
OR
<cfdump var="#data_query#" abort>
Will give you a beautiful display of came back from your query, or in any variable or structure.

ColdFusion query dropdownlist

Is it possible to populate a dropdown list with query results? For example with this output: Peps Company - AL ie (Company and State) separated with a hyphen.
Edit: Sorry for leaving out code. There is only one datasource.
<cfquery name="CompanyInfo" datasource=>
SELECT company, state
FROM clients
WHERE serv_billing = 1
AND status = 'Active'
ORDER BY Company
</cfquery>
<FORM METHOD="POST" ACTION="nextpage.cfm">
<SELECT name="company">
<CFOUTPUT QUERY="CompanyInfo">
<OPTION value="#CompanyInfo.company#">#CompanyInfo.company# - #CompanyInfo.state#</OPTION>
</CFOUTPUT>
</SELECT>
<INPUT TYPE="submit" VALUE="Submit Company">
</FORM>
Would this code give me the desired format for the dropdown list items ie Peps - AL?
?
The answer is yes. This code will do precisely that.
Does it not work? Do you have a problem with it, or..? I find it strange that you didn't simply try it out because you already seem to have the code to do what you want to do.
Seybsen's answer is technically correct, however, I would compel you to follow best practices and perform a single loop, rather than iterative returns to the database on each row result of the main query:
<CFQUERY name="qCompanies" datasource="yourdsn">
SELECT companies.id, companies.company, states.state_code
FROM companies
INNER JOIN states ON (companies.state_id = states.state_id)
</CFQUERY>
<SELECT name="company">
<CFOUTPUT QUERY="qCompanies">
<OPTION value="#qCompanies.id#">#qCompanies.company# - #qCompanies.state_code#</OPTION>
</CFOUTPUT>
</SELECT>
You can do it with cfloop like this:
<cfquery name="CompanyInfo" datasource="yourdsn">
SELECT company, state
FROM clients
WHERE serv_billing = 1 AND status = 'Active'
Order by Company
</cfquery>
<FORM METHOD="POST" ACTION="nextpage.cfm">
<SELECT name="company">
<CFLOOP QUERY="CompanyInfo">
<OPTION value="#CompanyInfo.company#">#CompanyInfo.company# - #CompanyInfo.state#</OPTION>
</CFLOOP>
</SELECT>
<INPUT TYPE="submit" VALUE="Submit Company">
</FORM>