Set ID and Text column Name in ColdFusion autoSuggest - coldfusion

I am using following code in ColdFusion to implement autosuggest.
<cfquery name="Site" datasource="Site">
SELECT ColumnName
FROM tableName
</cfquery>
<cfinput name="ColumnName"
type="text"
autoSuggest="#valueList(Site.ColumnName)#"
typeahead="true"
size="40"
placeholder="ColumnName"/>
It works perfectly, Is there any way to bind ID column? I want to submit the ID and have to display the text in auto suggest.
I meant, when we populate the select in html, we have id and text field. But seems to be missing in autosuggest in coldfusion
Is that possible in ColdFusion?

Related

how can I check if a record already in database before submitting form in ColdFusion applications

<cfquery datasource = "myDb" name = "compare">
select *
from users
where cnic = #form.cnic#
</cfquery>
<cfif compare.cnic eq form.cnic>
<p> *CNIC already Exists </p>
</cfif>
I think you're misstating the problem. It should be more like,
"How can I show a form to add a record I know is not in the database?"
Please clarify if that is not the case.
Based on your code, I assume there's been a form submission from another page already. You're running a query to see if there is a record in the users table where cnic = #form.cnic#. If there was no previous form submission, then form.cnic wouldn't exist.
<cfquery datasource="myDb" name="compare">
select *
from users
where
cnic = #form.cnic#
</cfquery>
So when this page loads, you've done your "check if a record is already in the database" with the query named compare. Now all you need to do is check if there are 0 records in the query.
<cfif compare.recordcount EQ 0>
<!--- Display form here. ---->
</cfif>
If the query returns any records, then there is at least one record in the database, so no need to show the form or allow it to be submitted.
You can use bellow code in Jquery ajax calling
<cfquery datasource = "myDb" name = "compare">
select *
from users
where cnic = #form.cnic#
</cfquery>
<cfif compare.recordcount GT 0>
<p> *CNIC already Exists </p>
</cfif>
Depending on your database, too, there are other options. MySQL has some features for INSERT ON DUPLICATE KEY UPDATE (https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html)
In MSSQL you can do:
IF NOT EXISTS (SELECT 1 FROM [users] u WITH (NOLOCK) WHERE cnic = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#form.cnic#")
INSERT INTO [users].....
(My cfsqltype syntax might not be 100% correct, but always use . Always.
Those might give you some other ways to handle your scenario.

Cfloop only returning values for the first and last values of a list

Long story short, I'm working on an application where CRUD is performed against a MySQL database. Everything is working great, except on the updating portion. I have an HTML form that the user submits to the database, and a separate page where a specific record (event) is pulled back into the form and populates the inputs. All of the other inputs are working properly, but my checkbox block is struggling.
Each checkbox input tag has this associated ColdFusion code (cfloop, cfif). The loop determines if the value is included in the list returned from the database and displays "Checked" if true:
<!--Each input has a cfloop and cfif tag to determine checked checkboxes-->
<input
type="checkbox"
id="alumni_relations"
value="Alumni Relations"
name="cboxes"
<cfloop index="foundRecord.event_type" list="#foundRecord.event_type#" delimiters=",">
<cfoutput>
<cfif #foundRecord.event_type# EQ "Alumni Relations">Checked</cfif>
</cfoutput>
</cfloop>
>
<label for="alumni_relations">Alumni Relations</label>
<br>
It works great! But for whatever reason it is only returning "Checked" for the first listed, and last listed item in the list (e.g a list of: Alumni Relations,Athletics,Management Society,Colleges will only return "Checked" for Alumni Relations and Athletics) Here's a picture of that:
Checkbox Example Image
I'm thinking it has to do something with the index, but I've scoured the documentation for cfloop and I'm just not figuring it out. Anyone with any help/advice would be much appreciated!

How to set value from a query to an input textbox

When a user clicks edit of a specific row in a table, I want the input textboxes in the same page to be filled with that row's details. I tried this but it did not work.
<cfquery name="getDataForEdit" datasource="dsn">
select * from usercardetails where id = '#url.id#'
</cfquery>
<cfoutput>
<cfset #form.username# = #getDataForEdit.username#>
</cfoutput>
Try this as an example to get you going....
<cfquery name="getDataForEdit" datasource="dsn">
select * from usercardetails where id = '#url.id#'
</cfquery>
<!---
OPTIONAL IMPROVEMENT: You might change your SQL to use CFQueryParam, this will protect against SQL injection
select * from usercardetails where id = <cfqueryparam cfsqltype="CF_SQL_NUMERIC" value="#url.id#" />
--->
<!---
OPTIONAL FOR DEBUGGING: If you uncomment this block it will show you the results of your query.
<cfoutput><cfdump var="#getDataForEdit#" label="getDataForEdit" expand="No" /><br /></cfoutput>
--->
<cfoutput>
<cfif getDataForEdit.recordcount is 1> <!--- Check that you only get one row back in results --->
<!--- Coldfusion will build forms for you using cfform, but many coders keep away from it, so instead you need to build the HTML of your form yourself. --->
<form action="index.cfm" method="post">
<p><label for="username">Username</label><input type="text" id="username" name="username" value="#getDataForEdit.username#" /></p>
<p><input type="submit" id="butty01" name="butty01" value="Go" /></p>
</form>
<cfelseif getDataForEdit.recordcount is 0> <!--- If no results --->
<p>No records found</p>
<cfelse> <!--- Anything else will mean many results --->
<p>An error occurred (Multiple records with this ID found)</p>
</cfif>
</cfoutput>
I've put comments in the code around some optional improvements.
Also note that...
<cfset #form.username# = #getDataForEdit.username#>
will be causing you problems.
<cfset username = getDataForEdit.username>
Will create/set a variable username equal to the value of the username column in your query. The # tags are not necessary in this context. # prints out the value of a variable, so you could have done this...
<cfset username = "#getDataForEdit.username#">
Which would print the value of the username column into a string of text (the only thing in the string being the value), and the string of text would have been assigned to the variable username.
form
is a reserved word, as it is a structure (it's worth looking up structures) that contains all of the form variable data posted to your page. You can inspect form by using cfdump
<cfoutput><cfdump var="#form#" label="form" expand="No" /><br /></cfoutput>
To print out one of the values inside form you can do
#form.username#
So (and very confusingly when you're a beginner)...
<cfset #form.username# = #getDataForEdit.username#>
Will create a variable with the name that corresponds to the value of the form field username posted to your page, and fill it with the username from your query. You really don't want that.
Keep at it. Once you get a few basic concepts sorted Coldfusion is a lovely language to code in.

How do you link a menu to a table from a query?

From the menu I have
<cfset temp =valuelist(GetDeptNum.csedept_name)>
<cfset ChangedElement = ListGetAt(temp, 2)>
<cfset TempToo = ListSetAt(temp, 2, ",")>
<cfdump var=#TempToo#>
<li class="fullChild"><a id="corporate" href="phonelistsearch_test.cfm?corporate" onclick="changeClass('corporate')" >Corporate</a><a class="opener"></a>
<ul>
<cfloop From = "1" To = "#ListLen(temptoo)#" INDEX = "Counter">
<cfoutput><li> #ListGetAt(temptoo, Counter)#
</cfoutput>
</cfloop>
</ul>
</li>
I have a table which I use a filter to search the table, working, see http://jsfiddle.net/45grrk3m/ .
How would I link it to a table so when I click on the plus sign I can choose different choices
and it will only filter those rows in that csedept_name I click?
Both tables GetUsers and GetDeptNum share the csedept_id collumn.
So from the menu dropdown I get the name right now which works GetDeptNum.csedept_name,
but not sure how I can make the connection between the menu and table.
You need to think about how you're generating the table.
Each option in your table should be a hyperlink back to the page (or ajax....) which passes the option selected. Your SQL query would then refine the search, and the table is recreated using the refined resulty

Getting the Value of the Query to Populate Form Fields

I had a Query Where i am Getting 14 records and i need to Populate this records in 4 form fields, they are coming as
select id, name, value from mytable
check the attached Screenshot.
I cannot do cfoutput query as it will loop the form fields 14 times,
i tried something like
queryname.name[currentow], but it not worked, checked this URL of cflib http://cflib.org/udf/queryGetRow
but that did not seems to get work. need to populate the value of the name column in the form field
need to know what is the right way
From this comment only,
"honestl my friend, i think i got confused what to ask; I will try to explain again: Auume i have two columns in my table {name & Value}. It has four rows. Now i have to populate the form fields of the rows 1 to 4, which contains the the details from the value column of the table"
I would do this to build my form:
<cfquery name="data" datasource="whatever">
select name, value
from mytable
</cfquery>
<form action="somewhere.cfm" method="post">
<cfoutput query="data">
<input name="name_#name# value="#value#">
</cfoutput>
</form>
Processing the form is outside the scope of the question so I won't do it. However, I will mention some potential problems. If name is a text field, and it's values contain spaces, puncuation marks, etc, your page will crash due to an invalid variable name. If your table has an id field that is either uuid or numeric, that's a better field to use.
Ok, I did it, Here is if someone encounters this, i may not be able to correctly tell what is required. So here is my Solution:
<cfset rsSettings= initial.getSettings()>
<cfloop array="#rsSettings#" index="k">
<cfset settings[k['name']] = k['value']>
</cfloop>
My Query is returning me an array, Using ben's post
http://www.bennadel.com/blog/124-Ask-Ben-Converting-a-Query-to-an-Array.htm
so to fill the form i used like this and it worked
<input type="text" name="pri" class="text" value="<cfoutput>#settings['pri']#</cfoutput>" />
if you guys have better solution, please share