你会如何​​去执行基于从表单中选择元素值的数据库查询?你会、表单、数据库查询、元素

2023-09-10 17:14:05 作者:朕已经忘了大明湖畔了

我是用ColdFusion作为我的应用程序服务器和SQL Server数据库。我列出了一些车辆的选择表单元素:沃尔沃S60 BMW M6 大众捷达

基于什么样的车辆用户选择,我需要我的网页来执行数据库查询,找出车辆的型,他们选择了

例如, SUV,跑车,敞篷车。取决于什么类型'被从数据库返回,该数据库将返回适于该车辆类型的选项列表。我的数据库表可根据车辆下拉的价值做到这一点,使一切都很好。

现在的话,我想现在列出该车辆的类型为一组复选框的可用选项。这样做应该是通过数据库结果集循环和产生的每一行的复选框的一个简单的例子。

我想这样做,而无需刷新页面。 如何动态地获取从下拉列表中的值,这个值传递到数据库,得到的结果回来,然后显示相应的复选框?

解决方案

我在前面的评论中提到,要做到这一点ColdFusion的最简单的方法是绑定你的表单元素CFC方法。在cfinput绑定谷歌搜索会导致很多的例子,但因为我被要求提供一个答案,我将展示一个例子,我曾这样写道。这不正是OP想要的东西,但它显示的总体思路。根据其他的值将填充一个文本框。

请注意,CFC和CFM文件必须在同一目录下。

.CFM文件

 < ---当您在此处键入一个诊所code:---->
< D​​IV ID =诊所codeInput级=隐藏>
诊所$ C $℃下输入名称=诊所code型=文本/>
< / DIV>

<!----一个查询结果会出现在这里---->
< D​​IV ID =clinicNameFromPatientSatisfaction级=隐藏>
病人满意度姓名和LT; cfinput类型=文本
NAME =NameOfClinic
绑定=CFC:PatientSatisfactionClinics.GetClinicName({诊所code})
bindonload =无>
< / DIV>
 

.cfc文件

 < cffunction名=GetClinicName访问=远程的返回类型=字符串>
< cfargument名=诊所code型=字符串要求=是>
< CFSCRIPT>
变种clinicName = QueryNew(一);
VAR returnString =无记录的诊所code&放大器; arguments.clinic code和; 。;
VAR诊所codeAsInt = 0;

如果(ISNUMERIC(arguments.clinic code)
而圆(arguments.clinic code)为arguments.clinic code)
诊所codeAsInt = arguments.clinic code;
< / CFSCRIPT>

< CFIF诊所codeAsInt GT 0 GT;
< CFQUERY NAME =clinicName数据源=德国之声>
选择名称
从patient_satisfaction_clinic
其中,clinic_ code =
< cfqueryparam cfsqltype =cf_sql_integer值=#诊所codeAsInt#>
< / CFQUERY>

< CFIF clinicName.recordcount GT 0 GT;
&所述; CFSET returnString = clinicName.name [1]≥
< / CFIF>
< / CFIF> <!---诊所codeAsInt GT 0 --->

< cfreturn returnString>

< / cffunction>
 

I am using ColdFusion as my application server and SQL Server for the database. I have a select form element which lists a number of vehicles: Volvo S60, BMW M6, VW Jetta.

Based on what vehicle the user selects, I need my webpage to perform a database query to find out what 'type' of vehicle they selected e.g. SUV, Coupe, Convertible. Depending on what 'type' is returned from the database, the database will return a list of options suitable for that vehicle type. My database tables can do this based on the vehicle drop-down's value so that's all good.

Now then, I want to now list the available options for that vehicle 'type' as a group of checkboxes. Doing this should be a simple case of looping through the database resultset and generating a checkbox for each row.

I want to do this without refreshing the page. How do I dynamically get the value from the drop-down, pass this value to the database, get the result back and then show the appropriate checkboxes?

解决方案

I mentioned in my earlier comment that the simplest way to do this in ColdFusion was to bind your form elements to cfc methods. A google search on "cfinput bind" will lead to lot's of examples, but since I was asked to provide an answer, I'll show an example I once wrote. It's not exactly what the OP wants, but it shows the general idea. It will populate one text box based on the value of another.

Note that the cfc and cfm files have to be in the same directory.

.cfm file

<!--- When you type a clinic code here: ---->
<div id="clinicCodeInput" class="hidden">
Clinic Code <input name="clinicCode" type="text" />
</div>

<!---- A query result will appear here ---->
<div id="clinicNameFromPatientSatisfaction" class="hidden">
Patient Satisfaction Name <cfinput type="text" 
name="NameOfClinic" 
bind="cfc:PatientSatisfactionClinics.GetClinicName({clinicCode})" 
bindonload="no"> 
</div>

.cfc file

<cffunction name="GetClinicName" access="remote" returntype="string">
<cfargument name="clinicCode" type="string" required="yes">
<cfscript>
var clinicName = QueryNew("a");
var returnString = "No Record for Clinic Code " & arguments.clinicCode & ".";
var clinicCodeAsInt = 0;

if (isNumeric(arguments.clinicCode) 
and round(arguments.clinicCode) is arguments.clinicCode)
clinicCodeAsInt = arguments.clinicCode;
</cfscript>

<cfif clinicCodeAsInt gt 0>
<cfquery name="clinicName" datasource="dw">
select name
from patient_satisfaction_clinic
where clinic_code = 
<cfqueryparam cfsqltype="cf_sql_integer" value="#clinicCodeAsInt#">
</cfquery>

<cfif clinicName.recordcount gt 0>
<cfset returnString = clinicName.name[1]>
</cfif>
</cfif>  <!--- clinicCodeAsInt gt 0 --->

<cfreturn returnString>

</cffunction>