Copy link to clipboard
Copied
Hi,
I use coldfusion but i'm no expert i'm trying to populate 3 select field from a previous selection its year/vendor/model of cars to find the appropriate tire size.
I manage to find some code to make the first 2 to load but the 3rd one the model doesnt work here is my code what M I missing or doing wrong ?
<!DOCTYPE html>
<head>
<title>losd list test</title>
<CFQUERY NAME="getyear" datasource="dataname">
SELECT DISTINCT year
FROM carmodels
order by year DESC
</cfquery>
<cfset Start = 1>
<cfset Start2 = 1>
<cfset Start3 = 1>
<cfset End = getyear.recordcount>
<script language="javascript">
/*Fonction addLoadEvent */
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
/*Define the options*/
var tab1 = new Array("Sélectionner une année", <cfloop query = "getyear" startRow = "#Start#" endRow = "#End#"><CFOUTPUT>"#year#"<CFIF getyear.currentRow EQ #end#><CFELSE>, </CFIF></CFOUTPUT></cfloop>);
var tab2 = new Array();
tab2["year"] = new Array("Marque");
<cfloop query = "getyear" startRow = "#Start2#" endRow = "#End#"><CFOUTPUT><cfset lannee = '#year#'>tab2["#year#"] = new Array("Sélectionner une marque" <CFQUERY NAME="getvendor" datasource="dataname">SELECT DISTINCT vendor FROM carmodels WHERE year = '#lannee#' ORDER by vendor</cfquery><cfset End3 = getvendor.recordcount><cfloop query = "getvendor" startRow = "#Start2#" endRow = "#End3#">, "#getvendor.vendor#"</cfloop>);</CFOUTPUT></cfloop>
var tab3 = new Array();
tab3["vendor"] = new Array("Model");
<cfloop query = "getvendor" startRow = "#Start3#" endRow = "#End#"><CFOUTPUT><cfset levendor = '#vendor#'>tab3["#vendor#"] = new Array("Sélectionner un modèle" <CFQUERY NAME="getmodel" datasource="dataname">SELECT DISTINCT model FROM carmodels WHERE vendor = '#levendor#' AND year = #lannee# ORDER by model</cfquery><cfset End4 = getmodel.recordcount><cfloop query = "getmodel" startRow = "#Start3#" endRow = "#End4#">, "#getmodel.model#"</cfloop>);</CFOUTPUT></cfloop>
/*The function to load the list*/
<!------ SELECT 1 ----------------->
function loadList1() {
var list1 = document.getElementById("list1");
for (var i = 0; i < tab1.length; i++) {
var item = document.createElement('option');
item.value = tab1;
item.innerHTML = tab1;
if (i == 0) {
item.selected = true;
}
list1.appendChild(item);
}
if (tab1.length > 0) {
loadList2(tab1[0]);
}
}
<!------ SELECT 2 ----------------->
function loadList2(list1Value) {
var list2 = document.getElementById("list2");
var toDelete = list2.childNodes;
var tab = tab2[list1Value];
while (list2.hasChildNodes()) {
list2.removeChild(toDelete[0]);
}
if (tab) {
for (var i = 0; i < tab.length; i++) {
var item = document.createElement('option');
item.value = tab;
item.innerHTML = tab;
list2.appendChild(item);
}
list2.disabled = false;
}
else {
list2.disabled = true;
}
}
<!------ SELECT 3 ----------------->
function loadList3(list2Value) {
var list3 = document.getElementById("list3");
var toDelete = list3.childNodes;
var tab = tab3[list2Value];
while (list3.hasChildNodes()) {
list3.removeChild(toDelete[0]);
}
if (tab) {
for (var i = 0; i < tab.length; i++) {
var item = document.createElement('option');
item.value = tab;
item.innerHTML = tab;
list3.appendChild(item);
}
list3.disabled = false;
}
else {
list3.disabled = true;
}
}
</script>
</head>
<body>
<form action="search.cfm" method="post">
<script type="text/javascript">addLoadEvent(loadList1);</script><select id="list1" name="year" style="width: 180px;" onChange="loadList2(this.value);"></select>
<!---- Here i get all the years and its working --->
<select id="list2" name="vendor" style="width: 180px;" onChange="loadList3(this.value);"></select>
<!---- the list3.disabled = true; is activated and the field is grey out once I select a year the vendor list appears GREAT --->
<select id="list3" name="Model" style="width: 180px;"></select>
<!---- the list3.disabled = true; is not activated and once i select a vendor it stays empty and the list3.disabled = true; comes in fonction and the field is greyed out--->
</form>
</body>
</html>
Thank you in advence for your help
Next tips:
1) ensure that the annee database column is of integer type.
2) change the initialization code <cfset anneeArray[1][1]=""> to <cfset anneeArray[1][1]="0">
Copy link to clipboard
Copied
Apparently, no event is triggered when one clicks on the field <select id="list3">.
Copy link to clipboard
Copied
No event should be trigger a list of model should appear then on submit i have all 3 info i need to find the tire size
Copy link to clipboard
Copied
I don't understand what you mean. What do you wish to happen? What is actually happening?
Copy link to clipboard
Copied
I uploaded it on a server Check it out
http://mesclients.ca/sitepneus/
When you choose a year all the make for that year appear in the 2nd select box when you select the make all the models should appear in the 3rd one.
Copy link to clipboard
Copied
There might be an issue with the block entitled "Define the options". The Javascript events will update upon each onChange event. But it seems to me that the ColdFusion code used to define the values of tab1, tab2, tab3 will not be updated.
My suggestion is that you implement the queries, not in this CFM page, but in a function within a CFC. Then make an AJAX call to the function at this point in the Javascript, passing the selected value of year and vendor.
Copy link to clipboard
Copied
I manage to make it work takes way to long to load you are right o needed to find another way so i found this tutorial
http://forta.com/blo...Related-Selects
I started playing around with the code here : http://mesclients.ca...neus/index3.cfm
It takes a while to load and then it gives me this error message :
"Error","ajp-bio-8014-exec-1371","09/18/17","13:49:48",,"Element year is undefined in ARGUMENTS. The specific sequence of files included or processed is: E:\MYPATH\sitepneus\art.cfc, line: 46 "
at cfart2ecfc931360593$funcGETART.runFunction(E:\MYPATH\sitepneus\art.cfc:46)
****************************************************
CFM Page, i kept his media / art refence for now
<!DOCTYPE html>
<head><title>Test page </title></head>
<body>
<cfform>
<table>
<tr>
<td>Select year:</td>
<td><cfselect name="mediaid"
bind="cfc:art.getMedia()"
bindonload="true" /></td>
</tr>
<tr>
<td>Select Make:</td>
<td><cfselect name="artid"
bind="cfc:art.getArt({mediaid})" /></td>
</tr>
</table>
</cfform>
</body>
</html>
**********************************************
CFC Page
<cfcomponent output="false">
<cfset THIS.dsn="mydns">
<!--- Get array of media types --->
<cffunction name="getMedia" access="remote" returnType="array">
<!--- Define variables --->
<cfset var data="">
<cfset var result=ArrayNew(2)>
<cfset var i=0>
<!--- Get data --->
<CFQUERY NAME="data" datasource="#THIS.dsn#">
SELECT DISTINCT year
FROM tx_carmodels
order by year DESC
</cfquery>
<!--- Convert results to array --->
<cfloop index="i" from="1" to="#data.RecordCount#">
<cfset result[2]=data.year>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
<!--- Get art by media type --->
<cffunction name="getArt" access="remote" returnType="array">
<cfargument name="mediaid" required="true">
<!--- Define variables --->
<cfset var data="">
<cfset var result=ArrayNew(2)>
<cfset var i=0>
<!--- Get data --->
<CFQUERY NAME="data" datasource="#THIS.dsn#">
SELECT DISTINCT vendor
FROM tx_carmodels
WHERE year = "#ARGUMENTS.year#"
order by year vendor
</cfquery>
<!--- Convert results to array --->
<cfloop index="i" from="1" to="#data.RecordCount#">
<cfset result[2]=data.vendor>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
</cfcomponent>
Why isnt it getting the year and why is it so long to load
Copy link to clipboard
Copied
You mix bits of Ben Forta's example with your code. That makes it difficult to debug.
I shall now give you a correction of your code. I hope it answers all your questions.
CFM
<!--- To properly display French words such as Sélectionner --->
<cfprocessingdirective pageencoding="utf-8">
<cfform>
<table>
<tr>
<td>Select Year:</td>
<td><cfselect name="annee"
bind="cfc:cars.getYear()"
bindonload="true" /></td>
</tr>
<tr>
<td>Select Vendor:</td>
<td><cfselect name="levendor"
bind="cfc:cars.getVendor({annee})" /></td>
</tr>
<tr>
<td>Select Model:</td>
<td><cfselect name="modele"
bind="cfc:cars.getModel({annee},{levendor})" /></td>
</tr>
</table>
</cfform>
cars.CFC
<cfcomponent>
<cfset this.dsn = "mydns">
<cfprocessingdirective pageencoding="utf-8">
<cffunction name="getYear" access="remote" returntype="array">
<cfset var yearQry = "">
<cfquery name = "yearQry" dataSource = "#THIS.dsn#">
SELECT DISTINCT year
FROM carmodels
ORDER BY year
</cfquery>
<!--- Values for the first - the default - select option --->
<cfset yearArray[1][1]="">
<cfset yearArray[1][2]="Sélectionner une année">
<cfloop query="yearQry">
<cfset yearArray[currentrow+1][1]=yearQry.year>
<cfset yearArray[currentrow+1][2]=yearQry.year>
</cfloop>
<cfreturn yearArray>
</cffunction>
<cffunction name="getVendor" access="remote" returntype="array">
<cfargument name="yr" required="true" type="string">
<cfset var vendorQry = "">
<cfset var vendorArray = arrayNew(2)>
<cfquery name = "vendorQry" dataSource = "#THIS.dsn#">
SELECT DISTINCT vendor
FROM carmodels
WHERE year = #arguments.yr#
ORDER BY vendor
</cfquery>
<!--- Values for the first - the default - select option --->
<cfset vendorArray[1][1]="">
<cfset vendorArray[1][2]="Sélectionner une marque">
<cfloop query="vendorQry">
<cfset vendorArray[currentrow+1][1]=vendorQry.vendor>
<cfset vendorArray[currentrow+1][2]=vendorQry.vendor>
</cfloop>
<cfreturn vendorArray>
</cffunction>
<cffunction name="getModel" access="remote" returntype="array">
<cfargument name="yr" required="true" type="string">
<cfargument name="vendor" required="true" type="string">
<cfset var modelQry = "">
<cfset var modelArray = arrayNew(2)>
<cfquery name = "modelQry" dataSource = "#THIS.dsn#">
SELECT DISTINCT model
FROM carmodels
WHERE year = #arguments.yr# AND vendor = '#arguments.vendor#'
ORDER BY model
</cfquery>
<!--- Values for the first - the default - select option --->
<cfset modelArray[1][1]="">
<cfset modelArray[1][2]="Model">
<cfloop query="modelQry">
<cfset modelArray[currentrow+1][1]=modelQry.model>
<cfset modelArray[currentrow+1][2]=modelQry.model>
</cfloop>
<cfreturn modelArray>
</cffunction>
</cfcomponent>
Copy link to clipboard
Copied
Hi,
it is working but I get 2 error message before it does :
http://mesclients.ca/sitepneus/index4.cfm
"Error","ajp-bio-8014-exec-1393","09/19/17","07:16:56",,"Error Executing Database Query.You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AND vendor = '' | order by model' at line 5 The specific sequence of files included or processed is: D:\MyPATH\sitepneus\cars.cfc, line: 117 " |
at cfcars2ecfc125460375$funcGETMODEL.runFunction(D:\MyPATH\sitepneus\cars.cfc:117)
"Error","ajp-bio-8014-exec-1394","09/19/17","07:16:56",,"Error Executing Database Query.You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'order by vendor' at line 7 The specific sequence of files included or processed is: D:\MyPATH\sitepneus\cars.cfc, line: 67 "
How can i fix this ?
Thank you so much
Copy link to clipboard
Copied
Ideally, the queries should be referring to vendor ID and model ID. You are using the names directly, so I went with that.
Following your original post, I am assuming the table has a column named vendor. I also assuming that the column contains the names of the vendors. Correct?
Copy link to clipboard
Copied
Error invoking CFC /sitepneus/cars.cfc : Internal Server Error [Enable debugging by adding 'cfdebug' to your URL parameters to see more information]
A possible cause of this error is that the CFM page is not importing or calling ColdFusion's Javascript libraries correctly. I'll assume the import is fine because it works eventually. It means you're probably not invoking the CFC correctly.
Verify that you have typed these lines correctly:
<cfselect name="annee"
bind="cfc:cars.getYear()"
bindonload="true" />
<cfselect name="levendor"
bind="cfc:cars.getVendor({annee})" />
<cfselect name="modele"
bind="cfc:cars.getModel({annee},{levendor})" />
There might also be errors in your CFC. Could you show us the code?
Copy link to clipboard
Copied
Don't use 'year' as the name of a field or variable. It is a reserved word in ColdFusion, representing a function.
Copy link to clipboard
Copied
Update: I tested your CFC by making the following direct calls
http://mesclients.ca/sitepneus/cars.cfc?method=getYear
http://mesclients.ca/sitepneus/cars.cfc?method=getVendor&yr=2017
http://mesclients.ca/sitepneus/cars.cfc?method=getModel&yr=2017&vendor=Nissan
I got no problems at all. So the most likely issue I can think of at the moment is the 'year' issue in my last post. Change the name to 'annee' and tell us if the problem remains.
Copy link to clipboard
Copied
I changed it to annee with the same error message
"Error","ajp-bio-8014-exec-1474","09/20/17","08:15:56",,"Error Executing Database Query.You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'order by vendor' at line 7 The specific sequence of files included or processed is: D:\MyPath\sitepneus\cars.cfc, line: 67 "
at cfcars2ecfc125460375$funcGETVENDOR.runFunction(D:\MyPath\sitepneus\cars.cfc:67)
"Error","ajp-bio-8014-exec-1484","09/20/17","08:15:56",,"Error Executing Database Query.You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AND vendor = '' order by model' at line 5 The specific sequence of files included or processed is: D:MyPath\sitepneus\cars.cfc, line: 117 "
at cfcars2ecfc125460375$funcGETMODEL.runFunction(D:\MyPath\sitepneus\cars.cfc:117)
here is the code :
CFM
****************************
<cfform>
<table>
<tr>
<td>Select Year :</td>
<td><cfselect name="annee"
bind="cfc:cars.getannee()"
bindonload="true" /></td>
</tr>
<tr>
<td>Select Vendor:</td>
<td><cfselect name="vendor"
bind="cfc:cars.getVendor({annee})" /></td>
</tr>
<tr>
<td>Select Model:</td>
<td><cfselect name="model"
bind="cfc:cars.getModel({annee},{vendor})" /></td>
</tr>
</table>
</cfform>
*********************************
CFC
********************************
<cfcomponent>
<cfset this.dsn = "Mydata">
<cfprocessingdirective pageEncoding="utf-8">
<cffunction name="getannee" access="remote" returntype="array">
<cfset var anneeQry = "">
<cfquery name = "anneeQry" dataSource = "#THIS.dsn#">
SELECT DISTINCT annee
FROM tx_carmodels
order by annee DESC
</cfquery>
<!--- Values for the first - the default - select option --->
<cfset anneeArray[1][1]="">
<cfset anneeArray[1][2]="Sélectionner une année">
<cfloop query="anneeQry">
<cfset anneeArray[currentrow+1][1]=anneeQry.annee>
<cfset anneeArray[currentrow+1][2]=anneeQry.annee>
</cfloop>
<cfreturn anneeArray>
</cffunction>
<cffunction name="getVendor" access="remote" returntype="array">
<cfargument name="yr" required="true" type="string">
<cfset var vendorQry = "">
<cfset var vendorArray = arrayNew(2)>
<cfquery name = "vendorQry" dataSource = "#THIS.dsn#">
SELECT DISTINCT vendor
FROM tx_carmodels
WHERE annee = #arguments.yr#
order by vendor
</cfquery>
<!--- Values for the first - the default - select option --->
<cfset vendorArray[1][1]="">
<cfset vendorArray[1][2]="Sélectionner une marque">
<cfloop query="vendorQry">
<cfset vendorArray[currentrow+1][1]=vendorQry.vendor>
<cfset vendorArray[currentrow+1][2]=vendorQry.vendor>
</cfloop>
<cfreturn vendorArray>
</cffunction>
<cffunction name="getModel" access="remote" returntype="array">
<cfargument name="yr" required="true" type="string">
<cfargument name="vendor" required="true" type="string">
<cfset var modelQry = "">
<cfset var modelArray = arrayNew(2)>
<cfquery name = "modelQry" dataSource = "#THIS.dsn#">
SELECT DISTINCT model
FROM tx_carmodels
WHERE annee = #arguments.yr# AND vendor = '#arguments.vendor#'
order by model
</cfquery>
<!--- Values for the first - the default - select option --->
<cfset modelArray[1][1]="">
<cfset modelArray[1][2]="Sélectionner un modele">
<cfloop query="modelQry">
<cfset modelArray[currentrow+1][1]=modelQry.model>
<cfset modelArray[currentrow+1][2]=modelQry.model>
</cfloop>
<cfreturn modelArray>
</cffunction>
</cfcomponent>
Thanks
Copy link to clipboard
Copied
I just tryed this just in case i couldnt use vendor either same error message
CFm
*********************
<cfform>
<table>
<tr>
<td>Select Year :</td>
<td><cfselect name="annee"
bind="cfc:cars.getannee()"
bindonload="true" /></td>
</tr>
<tr>
<td>Select Vendor:</td>
<td><cfselect name="levendor"
bind="cfc:cars.getVendor({annee})" /></td>
</tr>
<tr>
<td>Select Model:</td>
<td><cfselect name="model"
bind="cfc:cars.getModel({annee},{levendor})" /></td>
</tr>
</table>
</cfform>
The error says
Line 67
WHERE annee = #arguments.yr#
Line 117
WHERE annee = #arguments.yr# AND vendor = '#arguments.vendor#'
Is it possible that the cfc is loading the function before its called ?
Copy link to clipboard
Copied
Is it possible that the cfc is loading the function before its called ?
That is indeed possible. In fact I was thinking about something similar. I wondered whether ColdFusion is loading its Javascript libraries incorrectly.
What is your ColdFusion version? Did you change the location of any ColdFusion system files?
Copy link to clipboard
Copied
CF 11
and no
Copy link to clipboard
Copied
Thanks for the information. There seems to be a problem with the path. What happens when you replace cfc:cars with cfc:sitepneus.cars in each of the 3 bind attributes in the CFM?
Copy link to clipboard
Copied
same thing
http://mesclients.ca/sitepneus/index4.cfm
CFM
******************************
<cfform>
<table>
<tr>
<td>Select Year :</td>
<td><cfselect name="annee"
bind="cfc:sitepneus.cars.getannee()"
bindonload="true" /></td>
</tr>
<tr>
<td>Select Vendor:</td>
<td><cfselect name="vendor"
bind="cfc:sitepneus.cars.getVendor({annee})" /></td>
</tr>
<tr>
<td>Select Model:</td>
<td><cfselect name="model"
bind="cfc:sitepneus.cars.getModel({annee},{vendor})" /></td>
</tr>
</table>
</cfform>
Copy link to clipboard
Copied
Strange! I copied your exact code to my ColdFusion 2016 installation. I then created a MySQL database table, and filled in some values for year, vendor, model. It worked without any problems.
Let's start all over, reviewing all the evidence. My test suggests we look at ColdFusion 11 and/or at your individual setup. Combine this with the error messages you've been getting:
Line 67
WHERE annee = #arguments.yr#
Line 117
WHERE annee = #arguments.yr# AND vendor = '#arguments.vendor#'
Could it be that ColdFusion 11 is firing the last 2 functions even though there is no bindOnLoad attribute? This suggests a possible answer. Add the attribute bindOnLoad="false" to the last 2 cfselect tags:
<cfselect name="vendor"
bindOnLoad="false"
bind="cfc:sitepneus.cars.getVendor({annee})" />
<cfselect name="model"
bindOnLoad="false"
bind="cfc:sitepneus.cars.getModel({annee},{vendor})" />
Does that help?
Copy link to clipboard
Copied
Forgot to tell you i alredy tryed that same error
Copy link to clipboard
Copied
Next tips:
1) ensure that the annee database column is of integer type.
2) change the initialization code <cfset anneeArray[1][1]=""> to <cfset anneeArray[1][1]="0">
Copy link to clipboard
Copied
You wanna cry ?
annee/year is a varchar not a numeric field its working perfecly thank you so much
WHERE annee = '#arguments.yr#' AND vendor = '#arguments.vendor#'
Now why is <cfprocessingdirective pageencoding="utf-8"> not working ?
I even tryed using é ; for the é
Copy link to clipboard
Copied
I am glad we solved it. Overall, this was a good exercise in debugging. Nevertheless, I would advise you to look for a jQuery alternative for interacting dropdowns.
An alternative to <cfprocessingdirective pageencoding="utf-8">:
1) Open the ColdFusion Administrator
2) Go to the Java and JVM page. Click with the mouse at the end of text content in the field JVM Arguments. Type a space, followed by -Dfile.encoding=UTF8
3) Press the button to Submit Changes
4) Restart ColdFusion
Let us know whether it works.
Copy link to clipboard
Copied
Please kindly mark the correct answer. If you find none of the answers to be the correct one, you could also describe the solution then mark it as the correct answer. It will certainly help someone else in future.