Skip to main content
July 1, 2012
Answered

Bind in form works with cfinput, how to with cfselect and a href

  • July 1, 2012
  • 1 reply
  • 6926 views

Hi all

This code workks binding a cfinput in an HTML cfform:

Name:<cfinput type="text" name="FIRSTNAME" label="Name" required="yes" width="150" bind="{UsersGrid.FIRSTNAME}" >

However this href doesn't seem to bind correctly:

<a  href="http://www.google.com/search?hl=en&output=search&sclient=psy-ab&q={UsersGrid.COMPANY}&btnK=" bind="{UsersGrid.COMPANY}">Google.com</a>

This cfselect doesn't either:
Department: <cfselect name="DEPARTMENT1" width="100" size="1" label="Department" required="yes" bind="UsersGrid.DEPARTMENT1">
<option></option><cfoutput query="dNames"><option value="#DEPARTMENTIDS#">#DEPARTMENTNOM#</option></cfoutput>
</cfselect> 

How can I make them both work? Thank you

    This topic has been closed for replies.
    Correct answer itisdesign

    Great Aaaron the link works a treat! Thanks


    Excellent!  Now we'll try adding the cfselect.  Could you try this?

    <cfajaxproxy bind="javascript:myHandler({UsersGrid.COMPANY},{UsersGrid.SECTORS1})" />

    <script type="text/javascript">

      function myHandler(COMPANY,SECTORS1) {

          //update the link's href

          document.getElementById('myLink').href = 'http://www.coldfusion.com?q=' + COMPANY;

          //update SECTORS1's selected

          var theField = document.getElementById('SECTORS1');

          for(var i=0; i<theField.options.length; i++) {

              if(theField.options.value==SECTORS1) {

                  theField.options.selected = true;

              } else {

                  theField.options.selected = false;

              }

          }

      }

    </script>

    Sector:<cfselect class="input" name="SECTORS1" size="1" label="Sector" required="yes">

    <option></option><cfoutput query="industries"><option value="#SECTORSIDS#">#SECTORSNOM#</option></cfoutput></cfselect>

    <a href="" id="myLink" target="_blank">text</a>

    Thanks,

    -Aaron

    1 reply

    itisdesign
    Inspiring
    July 2, 2012

    goodychurro1 wrote:

    [...]

    this href doesn't seem to bind correctly:
    [...]

    This cfselect doesn't either:

    [...]

    How can I make them both work? Thank you

    Hi goodychurro1,

    The tag you're looking for is cfajaxproxy.  Here is a example to get you started (if you're on CF9, just remove the 3rd attribute from the queryNew() and then populate the query using a queryAddRow() and three querySetCell()s):

    <cfset q = queryNew("myID,myString", "integer,varchar", [[1,"one"],[2,"two"],[3,"three"]]) />

    <cfajaxproxy bind="javascript:myHandler({myGrid.myID})" />

    <script type="text/javascript">

      function myHandler(myID) {

          //update the link's href

          document.getElementById('myLink').href = 'http://www.coldfusion.com?q=' + myID;

          //update the select's selected

          var theField = document.getElementById('mySelect');

          for(var i=0; i<theField.options.length; i++) {

              if(theField.options.value==myID) {

                  theField.options.selected = true;

              } else {

                  theField.options.selected = false;

              }

          }

      }

    </script>

    <cfform>

      <cfgrid name="myGrid" format="html" width="200" query="q">

        <cfgridcolumn name="myID" />

        <cfgridcolumn name="myString" />

      </cfgrid>

      <cfselect name="mySelect" query="q" display="myString" value="myID" />

    </cfform>

    <a href="" id="myLink" target="_blank">text</a>

    Selecting a row in the grid changes the link's href and the select's selected option.

    References:

    - cfaxaxproxy

    - Binding data to form fields

    - a blog post that might be of help

    Thanks,

    -Aaron

    July 2, 2012

    Thanks Aaron really appreciated, I'll play around with it!!

    July 2, 2012

    Hi Aaron

    I put this code before my cfform tag, and changed the mygrid to the name of my grid (usersgrid):

    <cfajaxproxy bind="javascript:myHandler({UsersGrid.myID})" />

    <script type="text/javascript">

      function myHandler(myID) {

          //update the link's href

          document.getElementById('myLink').href = 'http://www.coldfusion.com?q=' + myID;

          //update the select's selected

          var theField = document.getElementById('mySelect');

          for(var i=0; i<theField.options.length; i++) {

              if(theField.options.value==myID) {

                  theField.options.selected = true;

              } else {

                  theField.options.selected = false;

              }

          }

      }

    </script>

    I then added this code in the cfform, but the link is shown as http://www.coldfusion.com?q=undefined - why is this? Thanks:

    <a href="" id="myLink" target="_blank">text</a>