0
votes

I am trying to validate text input, with a sql check. Based on the result from the ajax call, I want to return the output as red or green.

HTML:

<script>
$("#cdsid").click(function() {
    debugger;
    var value = $.trim($("#svcdsid").val());
    if (value != '') {
        $.ajax({
           type:"POST",
           url:"SVCDS_filter.cfc?method=SVCDSIDExists",
           data: "value",
           cache:false,
           dataType: "json",
           success: function(){
               alert('YES');
           },
           error: function(){
               alert('NO');
           }
        });
    } else {
         $("#targetDiv").html("Please Enter SSID");}
    });
</script>

I am getting "unexpected end of input". In debugger, after checking that the value is not null value, it skips to the end code. I am not sure why.

1
in debugger, after checking not null value, it skips to end code. not sure..why - user737665
try putting in alert(value) before your ajax call to see what is actually being passed as value and please let us know. Thanks - Mark Hart

1 Answers

0
votes

First You can Write a ajax function for checking your text input. In the ajax function you can pass the text value that you entered trough data. Url cfc function that you called for checking input text data,and the method is the name of your cfc function. Here it is "SVCDSIDExists". In the cfc file you can add new function that you checked for text value. The access="remote" for ajax. You can check in your table with the field . If it have a record it return 1 else it return 0. This value you can get from data of success in ajax. based on this value you can change the color of your input box.

<script>
$("#cdsid").click(function() {	
	var value = $.trim($("#svcdsid").val());
	if ($.trim(value).length != 0) {
		$.ajax({
			type:"POST",
			url:"SVCDS_filter.cfc?method=SVCDSIDExists",
			data: {
				value : value
			},
			cache:false,
			async : false,
			success: function(data){
			   if(data = 1){
					alert('YES');
					$("#targetDiv").css('border',"1px solid green")
				} else {
					alert('NO');
					$("#targetDiv").css('border',"1px solid red");
				}
			}
		});
	} else {
		 $("#targetDiv").html("Please Enter SSID");
	}
});
</script>
<cffunction name="SVCDSIDExists" returntype="any" access="remote" returnformat="plain" >		
	<cfargument name="value" type="string" default="" required="yes">	
	<cfset variables.returnVal = 0>
	<cfquery name="qGetSVCDSIDExists" result="result" datasource="#Application.ds#"  username="#Application.UserName#" password="#Application.Password#">
		select   columnname
		from  your_table
		where 1 = 1
		<cfif StructKeyExists(arguments,"value")>
			and columnname = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.value#">
		</cfif>
	</cfquery>
	<cfif qGetSVCDSIDExists.recordCount >
		<cfset variables.returnVal = 1 >	
	</cfif>
	<cfreturn variables.returnVal>
</cffunction>