Skip to main content
Known Participant
May 10, 2011
Question

need help in this jquery ajax response

  • May 10, 2011
  • 1 reply
  • 853 views

Hi Below is the code i got from internet trying to test but alwys getting false alert event the record does not xist in DB.

Below is the code

<script language="javascript" src="jquery-1.4.2.js"></script>

    <script type="text/javascript">
            $(document).ready(function() {    
            $('#username').blur(function() {    
                        $.ajax({            
                                type: "POST",
                                url: "checkUser.cfc?method=checkUsername",
                                data: "username=" + this.value,
                                datatype: "json",
                                success: function(response) {                                   
                                alert(response);                                                       
                                if (response == true) {
                                alert("true");   
                                //$('#usernameResponse').css('display', 'none');
                                //$('#submit').attr('disabled','disabled');
                                } else {
                                alert("flase");
                                //$('#usernameResponse').css('display', 'inline');
                                //$('#username').select();
                                //$('#submit').attr('disabled','disabled');
                                }     }                 });             });         });    
    </script>    

<form>     <div class="formcontainer">
                  <label>User Name:</label>
                  <input type="text" name="username" id="username" />
                  <span id="usernameResponse">Sorry, that username is taken</span>
                  </div>
                  <div class="formcontainer">
                      <label>Password:</label>
                      <input type="password" name="password" id="password" />
                   </div>     
             <div class="formcontainer">
            <input type="submit" id="submit" value="Submit" />
                </div>
                 </form>

CFC File:

<cfcomponent output="false">     
    <cffunction name="getAllUsernames" access="private" returntype="query" output="false">
                 <cfset var qGetAllUsernames = queryNew('userID,username') /> 
                 <cfset queryAddRow(qGetAllUsernames, 4) /> 
                 <cfset querySetCell(qGetAllUsernames, 'userID', 1, 1) />
                 <cfset querySetCell(qGetAllUsernames, 'userName', 'John', 1) />
                 <cfset querySetCell(qGetAllUsernames, 'userID', 2, 2) />
                 <cfset querySetCell(qGetAllUsernames, 'userName', 'Paul', 2) />
                 <cfset querySetCell(qGetAllUsernames, 'userID', 3, 3) />
                 <cfset querySetCell(qGetAllUsernames, 'userName', 'George', 3) />    
                 <cfset querySetCell(qGetAllUsernames, 'userID', 4, 4) />
                 <cfset querySetCell(qGetAllUsernames, 'userName', 'Ringo', 4) />
                 <cfreturn qGetAllUsernames />
    </cffunction> 
   
    <cffunction name="checkUsername" access="remote" output="false" returntype="string" returnformat="JSON">
        <cfargument name="username" type="string" default="" />
        <cfset var qAllUsernames = getAllUsernames() /> 
        <cfquery name="qCheckUsername" dbtype="query">
        SELECT * FROM qAllUserNames WHERE upper(username) = <cfqueryparam value="#ucase(arguments.username)#" cfsqltype="cf_sql_varchar" />
        </cfquery>
                
        <cfif qCheckUsername.recordcount>
        <cfreturn true>
        <cfelse>
        <cfreturn true>       
        </cfif>       
    </cffunction>   
</cfcomponent>

when i run cfc alone returns result like true or false but when i try to alert that response alert box of window size with all spaces and rsult comes so not able to tet if(reponse == true does not working.

any help will be great thanks

this is alert box poped with response

true

</td></td></td></th></th></th></tr></tr></tr></table></table></table></a></abbrev></acronym></address></applet></au></b></banner></big></blink></blockquote></bq></caption></center></cite></code></comment></del></dfn></dir></div></div></dl></em></fig></fn></font></form></frame></frameset></h1></h2></h3></h4></h5></h6></head></i></ins></kbd></listing></map></marquee></menu></multicol></nobr></noframes></noscript></note></ol></p></param></person></plaintext></pre></q></s></samp></script></select></small></strike></strong></sub></sup></table></td></textarea></th></title></tr></tt></u></ul></var></wbr></xmp>


This topic has been closed for replies.

1 reply

BKBK
Community Expert
Community Expert
June 3, 2011

cfnew wrote:

...
...

    <cffunction name="checkUsername" access="remote" output="false" returntype="string" returnformat="JSON">
...

...      
        <cfif qCheckUsername.recordcount>
        <cfreturn true>
        <cfelse>
        <cfreturn true>       
        </cfif>       
...

...


3 things. Do you mean perhaps:

1) name="IsUsernameValid"

2) returntype="boolean"

3) <cfif qCheckUsername.recordcount>
<cfreturn true>
<cfelse>
<cfreturn false>       
</cfif>

Owainnorth
Inspiring
June 3, 2011

You can also tidy up the method even more once you've made the changes BKBK has suggested:

<cfreturn qCheckUsername.recordcount />