Solved

How to pass a javascript variable to Cold fusion

Posted on 2010-11-16
4
341 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquery 10 20
DataTables + iCheck + pagination Issue 2 23
Insert query into temp tables using Coldfusion 3 21
Jquery Validation Messages Not Showing Up 3 15
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
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 …
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…

810 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