2014-03-25 57 views
0

我有問題。我有一個要求輸入一些基本輸入的coldfusion表格如何在按下提交按鈕之前查詢數據庫

<label class="ricRequired">ID<label><br /> 
<input type="text" id="tid" name="tid" size="30" maxlength="10" class="ricValidate" ric:required="yes" ric:format="integer" /> 

表單在結尾處有一個提交按鈕。一旦用戶輸入他/她的ID,我想檢查用戶是否存在於數據庫中。我怎樣才能做到這一點?有比使用ajax更簡單的方法嗎?

+1

只有在您願意進行頁面加載時才能避免Ajax。 –

+0

我將如何做頁面重新加載並保持已填充的表單域? – user2967577

+0

如果會涉及「POST」變量,然後用它們重新填充輸入字段。大多數情況下,ajax會大大簡化事情。以前你曾使用過它嗎? –

回答

0

我不確定你需要什麼,但我會給你一個我將如何去做的例子。

動作/ HTML頁面

<html> 
    <head> 
     <script type="text/javascript" src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> 
     <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script> 

     <script type="text/javascript"> 
      $(document).ready(function() { 
       $('#tid').autocomplete({ 
        source: function(query, response) { 
         $.ajax({ 
          url: "funcEntity.cfc?method=userSearch&returnformat=json", 
          dataType: "json", 
          data: { 
           searchPhrase: query.term 
          }, 
          success: function(result) { 
           $("#someDiv").html(result); 
          } 
         }); 
        } 
       }); 
     </script> 

    </head> 
    <body> 
     <div id="someDiv"> 
      <!--- displays message ---> 
     </div> 
     <div> 
      <form name="someForm" method="POST"> 
       <label class="ricRequired">ID<label><br /> 
       <input type="text" id="tid" name="tid" size="30" maxlength="10" class="ricValidate" ric:required="yes" ric:format="integer" /> 
      </form> 
     </div> 
    </body> 
</html> 

功能/ funcEntity.cfc

<cfcomponent> 

<cffunction name="userSearch" access="remote"> 
    <cfargument name="searchPhrase" /> 
    <cfquery name="usernameQuery" datasource="yourDatasource"> 
     SELECT username 
     FROM someTable 
     WHERE username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#searchPhrase#"> 
    </cfquery> 
    <cfif usernameQuery.recordCount GT "0"> 
     <cfset var content = "This username already exists."> 
    <cfelse> 
     <cfset var content = "This username is available."> 
    </cfif> 
    <cfreturn content> 
</cffunction> 

</cfcomponent> 

這不是測試代碼,我在趕時間排序的。我使用了自動完成功能,以便在您輸入時檢查數據庫。