Skip to main content
Inspiring
December 10, 2010
Answered

Query database "onblur" of textbox

  • December 10, 2010
  • 2 replies
  • 3119 views

Hi All,

As the titles suggests I have a form field field called 'formref' which is a cfinput field and of type text.  On blur or on leave of focus I would like to query a cfquery with the value of the textbox so I can see wether or not the user has entered a duplicate formref which already exists in the database and display the user a friendly error message to make them re-type another formref.

I am only getting back into coldfusion after many years out so would apprciate any help you guys can give

Many Thanks

George

    This topic has been closed for replies.
    Correct answer meensi

    Hi,

    You can use jQuery for this.

    or use Bind. For Bind here is the example

    This is my cfm file, where I will take the input.

    <cfform id="myForm" format="html">
            This is my edit box.<br />
            <cfinput type="text" name="myText">
        </cfform>
        <hr />
        And this is the bound div container.<br />
    <cfdiv bind="cfc:bindSample.getDivContent({myText})"></cfdiv>

    I will get the user input here and just print it.

    Here is the bindSample.cfc

    <cfcomponent output="false">
            <cffunction name="getDivContent" returntype="string"
                access="remote">
                <cfargument name="edit">
               
                    <cfreturn "This is the content returned from the CFC for
                        the div, the calling page variable is '<strong>#arguments.edit#</
                        strong>'.">
            </cffunction>
        </cfcomponent>

    2 replies

    Participating Frequently
    August 23, 2011

    How would you do this in MX7.  The cfc: way to invoke does not seem to work in MX7.  I am trying fill a text box after

    querying the database.

    Inspiring
    August 24, 2011

    Put your coldfusion stuff in a template and put that template into an iframe of your main page.  Use javascript to send data to the iframe page and to write back to the main page.

    meensiCorrect answer
    Inspiring
    December 10, 2010

    Hi,

    You can use jQuery for this.

    or use Bind. For Bind here is the example

    This is my cfm file, where I will take the input.

    <cfform id="myForm" format="html">
            This is my edit box.<br />
            <cfinput type="text" name="myText">
        </cfform>
        <hr />
        And this is the bound div container.<br />
    <cfdiv bind="cfc:bindSample.getDivContent({myText})"></cfdiv>

    I will get the user input here and just print it.

    Here is the bindSample.cfc

    <cfcomponent output="false">
            <cffunction name="getDivContent" returntype="string"
                access="remote">
                <cfargument name="edit">
               
                    <cfreturn "This is the content returned from the CFC for
                        the div, the calling page variable is '<strong>#arguments.edit#</
                        strong>'.">
            </cffunction>
        </cfcomponent>

    _G_1Author
    Inspiring
    December 10, 2010

    Hi Thanks for your sugestion it works great, and I am succesfully displaying an error message to the user if it is a duplicate formref.

    My only problem now is, how do I stop them from submitting the form if this error message exists ???

    _G_1Author
    Inspiring
    December 21, 2010

    Good suggestion, misleading explanation. 

    If the user has js disabled, the onBlur action would not have worked.  However, since it's a textbox, it's usually possible to submit the form with the enter key.  I don't know if this triggers the onBlur for that textbox or not.  The OP might want to test it.


    Guys still struggling with this.... I am displaying my message corerctly but I am struggling to utilize the return parameter on the parent page.

    Here is what I have so far.

    Parent Page

    <cfinput id="formref" name="formref" type="text" title="Form reference" />

    <cfdiv id="georgetest" bind="cfc:checkRef.getDivContent({formref})" bindonload="false"></cfdiv>

    checkRef.cfc

    <cfcomponent output="false">
    <cffunction name="getDivContent" returntype="string" access="remote">
      <cfargument name="edit">          
     
      <cfif len(arguments.edit) equals 11>
       <cfquery name="checkFormRef" datasource="#application.dsn#">
        SELECT    applicationid
        FROM      safegrd_owner.application
        WHERE     formref = '#arguments.edit#' and rownum = 1
       </cfquery>
      
       <cfif checkFormRef.recordcount gte 1>
        <cfreturn "<span style='color:red;font-weight:bold'>   #arguments.edit# is already in use. Please enter another reference.</span>">
       <cfelse>
        <cfreturn "<span style='color:green;font-weight:bold'>   #arguments.edit# is a valid form reference.</span>">
       </cfif>
      </cfif>
        </cffunction>
    </cfcomponent>

    Basically if, the reference is already in use then I want to disable a submit button on my parent page... if it is a valid reference then I will allow them to proceed and submit the form.

    If you could help I would be very grateful, this is my first time getting back into cf for a ong while so I am a bit rusty.

    Many Thanks,
    George