Skip to main content
BreakawayPaul
Inspiring
March 18, 2013
Answered

select/bind help

  • March 18, 2013
  • 1 reply
  • 6520 views

I'm helping a colleague with a web app that is basically a database of links to documents that are scattered around our website.  It's meant to be a way to search for documents using various criteria without having to browse the entire site.

The data is organized into categories (known as disciplines) and subcategories (known as subdisciplines) as well as a few other parameters.  I'm helping to get the database normalized, but one of the things I want to do is make the search form reflect the current database contents.

Currently, a web form with a few <select> items is used, and some Javascript makes the subdisciplines <select> dependent on what's selected in the disciplines <select> box. Thus, anytime a new one of either of those two items is added, the JS needs to be edited.  I'd like to make this automatic, so I'm trying to use bind to do it.

Right now I have the following form:

<cfform name="fectchdocs" action="#CGI.SCRIPT_NAME#" method="post">

<p><label for="disc">Discipline:</label><br />

<select name="disc" id="disc">

<option value="">All</option>

<cfoutput query="GetDisc">

<option value="#disc_id#"<cfif FORM.disc eq disc_id> selected="selected"</cfif>>#adiscipline#</option></cfoutput>

</select></p>

<p><label for="subdisc">Subdiscipline:</label><br />

<cfselect name="subdisc" id="subdisc" bindonload="Yes" bind="cfc:searchdata.getSubs({discs})" /></p>

<p><input type="select" name="search" value="Search" /></p>

</cfform>

And the following CFC (searchform.cfc):

<cfcomponent output="false">

    <cffunction name="getDiscs" access="remote" returntype="array">

        <cfquery name="discs" datasource="guidance">

            SELECT disc_id, adiscipline

            FROM search_fhwa_disc

            ORDER BY adiscipline

        </cfquery>

        <cfset count="1" />

        <cfset results = arraynew(2)>

        <cfloop query="discs">

            <cfset results[count][1]="#disc_id#" />

            <cfset results[count][2]="#adiscipline#" />

            <cfset count= count+1>

        </cfloop>

       

        <cfreturn results />

    </cffunction>

    <cffunction name="getSubs" access="remote" returntype="array">

        <cfargument name="disc_id" type="string" required="true">

        <cfquery name="subs" datasource="guidance">

            SELECT sub_id, bsubdiscipline

            FROM search_fhwa_sub

            WHERE parent_id = <cfqueryparam value="#arguments.disc_id#" cfsqltype="cf_sql_numeric">

            ORDER BY bsubdiscipline

        </cfquery>

        <cfset count="1" />

        <cfset results = arraynew(2)>

   

        <cfloop query="subs">

            <cfset results[count][1]="#sub_id#" />

            <cfset results[count][2]="#bsubdiscipline#" />

            <cfset count= count+1>

        </cfloop>

        <cfreturn results />

    </cffunction>

</cfcomponent>

It of course fails miserable with three popup errors.  A bind error telling me 'discs' is not found.  A second 'discs' not found error, and one about passing a null value to my query.

It's obvious that I'm using the wrong function/variable name somewhere, but I'm not sure where.

I know that the CFC works, because if I invoke either method, I get the array that I expect when I dump the returnvariable.

This topic has been closed for replies.
Correct answer Ashish_Todon

BreakawayPaul wrote:

Response:

"{\"COLUMNS\":[\"SUB_ID\",\"BSUBDISCIPLINE\"],\"DATA\":[[1506,\"Grant Programs\"],[1559,\"Program and Project Development\"],[1507,\"Truck Size and Weight\"]]}"

We are very much on right track you get result it means bind is OK, CFC query is OK

Steps to follow

1) Got to your Cf admin.

2) server settings > settings

3) Prefix serialized JSON with[//] option will be checked in your case please uncheck this and save. 

4) and check your page again.

BreakawayPaul wrote:

In the CFC, where we have this line: <cfreturn serializeJSON(subs)>

The value in the parens should be the name of the query in the CFC, correct?

yes

BreakawayPaul wrote:

I thought about just using JavaScript and having a query build the JS, but I'm pretty hopeless with JS, so I'd have to find a working example to edit.

Above step might have solve your problem. but we can discuss this(JS) way of doing our job. I will surly add code here. so we can discuss more


Hi

Above solution is by changing your server setting you can try that but we can do one more thing

Please see some changes in cfc level:-

Change 1 : I have added attribute returnformat="json" here.

Change 2 : I removed serializejson() function and return my result query as it is.

<cfcomponent>

          <cffunction name="getname" access="remote" securejson="false" returnformat="json"> //Change 1

                    <cfargument name="id" required="true" />

 

                    <cfquery name="qname" datasource="formexpress" >

                              select companyId as SUB_ID,

                                     companyname as BSUBDISCIPLINE

                                from company

                               where companyId = #ARGUMENTS.id#

                    </cfquery>

 

                    <cfreturn qname> // Change 2 

          </cffunction>

</cfcomponent>

By this you don't need to change your server settings

1 reply

Inspiring
March 18, 2013

When you first load your form, the value of ({discs}) will be an empty string.  You then pass this to your getSubs() method, and your query will crash.

To see why, invoke the method using coldfusion, but pass an empty string as your argument.

I rarely if ever do this sort of thing, but changing bindonload to false on your second select might solve the problem.

BreakawayPaul
Inspiring
March 18, 2013

Well that should have been pretty obvious, shouldn't it?

Changing bindonload to false eliminated two of the errors.  Now I just get a "bind failed, element not found: discs" error when I load the page.

Inspiring
March 18, 2013

Insted of creating 2-d array it would be better to use your query directly for binding cfselect please go through the code

This my cfm

<cfform name="test" >

          <cfselect name="desc" id="desc">

                    <cfoutput>

                  <cfloop index="i" from="1" to="10" >

                                        <option value="#i#">#i#</option>

                              </cfloop>

        </cfoutput>

          </cfselect>

          <cfselect name="company" id="company"

                                bind="cfc:Utility.test.getname({desc})" bindonload="false"

                                value="COMPANYID" display="COMPANYNAME"></cfselect>  // here value and display should match column name of your query

  </cfform>

This is CFC

<cfcomponent>

          <cffunction name="getname" access="remote" >

                    <cfargument name="id" required="true" />

                    <cfquery name="qname" datasource="" >

                              select companyId, companyname from company where companyId = #ARGUMENTS.id#

                    </cfquery>

                    <cfreturn serializeJSON(qname)>

          </cffunction>

</cfcomponent>