troubleshooting Question

CFC Lookup - When Not Found

Avatar of digitalwise
digitalwise asked on
ColdFusion LanguagejQuery
5 Comments2 Solutions188 ViewsLast Modified:
We have a CFC that we are using to look up a school - I want to limit it so they can't enter something that isn't in the lookup.

Form fields

input type="text" class="form-control" id="schoolname" name="schoolname" value="#getapp.showipeds#" />
    <input type="hidden" class="form-control" name="AI_IPEDS" id="AI_IPEDS" value="#getapp.AI_IPEDS#" />

jquery

$("#schoolname").autocomplete({
            source: function(request, response){
                $.ajax({
                    url: "ipeds.cfc?method=companyNameList",
                    dataType: "json",
                    data: {
                        searchterm: request.term
                    },
                    success: function(data){
                        response(data);
                    }
                })
            },
            minLength: 3,
            select: function(event, ui) {
                $('#AI_IPEDS').val(ui.item.id);
                $('#schoolname').val(ui.item.label);
            }
        });

CFC

<cffunction name="companyNameList" access="remote" output="false" hint="I return a list of companies with IDs" returnformat="JSON">
    <cfargument name="searchterm" required="false" default="" />

      <cfset var returnArray = arrayNew(1) />
    
     
<cfquery name="qryStates" dataSource="mcsf">
    Select schoolname + ' (' + schoolcity + ', ' + schoolstate + ')' as showme, ipedsid from College_IPEDS where schoolname like <cfqueryparam value="%#arguments.searchterm#%" cfsqltype="cf_sql_varchar"> order by schoolname, schoolstate
</cfquery>
 
 <CFIF qryStates.recordcount>
<cfloop query="qryStates">
    <cfset statesStruct = StructNew() />
    <cfset statesStruct["id"] = ipedsid />
    <cfset statesStruct["label"] = showme />
     
    <cfset ArrayAppend(returnArray,statesStruct) />
</cfloop>
<CFELSE>
<cfset statesStruct = StructNew() />
    <cfset statesStruct["id"] = 0 />
    <cfset statesStruct["label"] = showme />
     
    <cfset ArrayAppend(returnArray,statesStruct) />
</CFIF>
 
<cfreturn returnArray />
</cffunction>
ASKER CERTIFIED SOLUTION
dgrafx

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros