将cfquery放在Coldfusion表格中的位置

时间:2013-03-22 18:50:16

标签: coldfusion cfquery

我有cfquery使用WHERE子句中的一些FORM字段。我的第一个问题是,每次访问我的网页时,cfquery代码都会显示在页面顶部。我应该将查询放在.cfm表单中并访问表单中的某些字段?我的第二个问题是我不确定WHERE子句是否正在识别字段的值。你能帮帮我吗?

以下是我的代码设置方式:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<script language="javascript" type="text/javascript">
function addRow() { 
     var tbl = document.getElementById('tblSample');
     var lastRow = tbl.rows.length; 
     var iteration = lastRow - 3;
     var row = tbl.insertRow(lastRow);
     ........... etcetra.......
}
</script>
</head>
<body lang=EN-US style='tab-interval:.5in'>
<div class=Section1>
<cfparam name="awardTotals" default="0" />
<cfparam name="search_award.GrandTotal" default="0" />
<cfif isDefined("form.Finalize")>
   <cfquery name="search_award" datasource="Test">
    SELECT g.Code1 + g.Code2 + g.Code3 + g.Code4 AS GrandTotal
      FROM Codes g 
     WHERE g.CodeNumber = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.CodeNum#">
       AND g.TestYear = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.TestYear#">
       AND g.SelType = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.SelType#"> 
       AND g.Jurisdiction = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.SelJuris#">    
   </cfquery>
   <cfif not search_award.GrandTotal is FundingTotals>
      <script type="text/javascript">
         alert('The totals do not match.');
   return false;       
</script>
   <cfelse>
<script type="text/javascript">
   alert('The totals match.');
   return true;
</script>      
    </cfif>
</cfif>
<p class=MsoNormal align=center style='text-align:center'><
<cfoutput>
  <form name="thisform" action="FormData.cfm" method="post">
    <p class=MsoNormal align=left style='text-align:left'>
      <input type="hidden" id="totalFields" name="totalFields" value="0">
      <input type="text" id="awardTotals" name="#search_award.GrantTotal#" value="0">
      <table width="1177" border="1" id="tblSample">
          <tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>
          <th height="10"bgcolor="##cccccc" colspan="10"><h3>Jurisdiction:
             <select name="SelJuris" id="SelJuris">
                <option value = "0">---Jurisdictions---</option>
                <option value = "1">Allegany County</option>
                <option value = "2">Anne Arundel County</option>
                <option value = "3">Baltimore County</option>
                <option value = "4">Calvert County</option>
                <option value = "5">Caroline County</option>
             </select>
             <input name="CodeNum" id="CodeNum" type="text" size="20">  
             <input name="TestYear" id="TestYear" type="text" size="20"> 
             <input name="SelType" id="SelType" type="text" size="20">  
          </th>
          </tr>
</table>
<input type="Submit" name="Submit Form" value="Submit Form" onClick="">
<input type="Button" name="Finalize" value="Finalize" onClick="">
</form>
</body>
</cfoutput>
</html>

(从评论中更新)

现在我收到Element CODENUM is undefined in FORM

的消息

3 个答案:

答案 0 :(得分:3)

你想要这种逻辑。如果表单已提交,请使用它。在你的情况下,它会是这样的:

<cfif structkeyexists(form, "codenum")>
 query, process, display, etc
<cfif>
rest of page

此外,您还希望出于各种原因使用查询参数。所以这个:

g.CodeNumber = '#form.CodeNum#'

成为这个:

g.CodeNumber = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.CodeNum#">

除非它是一个数字,在这种情况下你改变了数据类型。

答案 1 :(得分:1)

您需要在cfquery周围添加条件。同时更新提交按钮的名称,不含空格

<input type="Submit" name="SubmitForm" value="Submit Form" onClick="">



   <cfif isDefined("form.SubmitForm")>

          <cfquery name="search_award" datasource="TrenaTest">
               SELECT g.Code1 + g.Code2 + g.Code3 + g.Code4 AS GrandTotal
                FROM Codes g 
              WHERE g.CodeNumber = '#form.CodeNum#'
              AND g.TestYear = '#form.TestYear#'
              AND g.SelType =  '#form.SelType#'
              AND g.JurisdictionID = '#form.SelJuris#'  
         </cfquery>

    </cfif>

答案 2 :(得分:-1)

请注意,在所有示例中,都使用了标准的html表单标记,但查询的条件在Coldfusion表单范围中定义。我相信您需要将<form>转换为<cfform><input>转换为<cfinput>,如:

      ...........
<cfinput type="Submit" name="SubmitForm" value="Submit Form" onClick="">
</cfform>
<cfif isdefined('form.submit')>
      .............