Solved

How to pass a javascript variable to Cold fusion

Posted on 2010-11-16
4
339 Views
Last Modified: 2012-05-10
I am using a jquery/ajax function to check a username for availability in a database table.  It is working great... but I need to know how to validate the form that the username field resides in.  Currently, if the cfc returns true... it adds text in a div (css driven). Perfect, but if the username is not available, I do not want the user to be able to submit the form.

Obviuosly I can validate forms, but how to do it in this instance??

Any help appreciated...
check.cfm:

_________________________



<script type="text/javascript">

chkUsernameUnique = function(theUsername){

	$.getJSON("/myCFC.cfc", {

		method: 'chkUsername',

		Username: theUsername,

		returnformat: 'json'

	}, function(isUsernameUnique){

	

		if (isUsernameUnique == true) {

			$("#theErrorDivID").html('Please select a new username, that one is taken');

		}

		else {

			$("#theErrorDivID").html('Username is available');

		}

	});

};

</script>



<div id="theErrorDivID"></div>

<input type="text" name="username" id="username" onchange="chkUsernameUnique(this.value);" />





++++++++++++++++++++++++++++++++++++++++++++



.cfc

_____________________

<cfcomponent>

<cffunction name="chkUsername" access="remote" returnformat="json" output="false">

		<cfargument name="Username" required="true">

			

		<cfquery name="chkUsername" datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">

		SELECT cst_ID FROM tbl_customers WHERE cst_username = <cfqueryparam value="#arguments.Username#" cfsqltype="cf_sql_varchar" />

		</cfquery>

		

		<cfreturn yesNoFormat(chkUsername.recordCount)>

	</cffunction>

</cfcomponent>

Open in new window

0
Comment
Question by:14_east
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
sam2912 earned 250 total points
ID: 34144818

if (isUsernameUnique == true) {
	$("#theErrorDivID").html('Please select a new username, that one is taken');
	$("input[type=submit]").attr("disabled", true);
}
else {
	$("#theErrorDivID").html('Username is available');
	$("input[type=submit]").attr("disabled", false);
}

Open in new window

0
 

Author Closing Comment

by:14_east
ID: 34145111
Hell yea, thanks man!!
0
 

Author Comment

by:14_east
ID: 34145291
Uh-oh... I spoke to soon.  It works fine, but I realized that my "submit" button is an image foeld.   will that make a difference?
0
 
LVL 14

Expert Comment

by:sam2912
ID: 34145336
It should not make a difference. However, if using an image, user may not know that the submit button is disabled as it looks the same, disabled or not.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now