Link to home
Start Free TrialLog in
Avatar of digitalwise
digitalwise

asked on

CFC Lookup - When Not Found

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#" />

Open in new window


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);
            }
        });

Open in new window


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>

Open in new window

SOLUTION
Avatar of James Rodgers
James Rodgers
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of digitalwise
digitalwise

ASKER

I need to be able to reset the value of the hidden field in case they come back and change it to something not in the list.   A warning will not do that.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!  I had to make a tweak in the CFC but that did the trick.
Glad it worked for you !