views:

76

answers:

2

I'm trying the following code to execute a search and it's not working. On the search.cfm page, the only value coming back is the value I input into the search field (even if I click an autosuggest value, it's not coming back; only the letters I actually type myself come back).

<cfform class="titleSearchForm" id="searchForm" action="search.cfm?GameID=#cfautosuggestvalue.GameID#" method="post">
    <fieldset>
        <cfinput type="text" class="titleSearchField" name="TitleName" onChange="form.submit()" autosuggest="cfc:gz.cfcomp.search.AutoSuggestSearch({cfautosuggestvalue})">
        <input type="button" class="titleSearchButton" value=" " />
    </fieldset>
</cfform>

Query in CFC:

    <cfquery name="SearchResult" datasource="myDSN">
        SELECT CONCAT(titles.TitleName, ' on ', platforms.PlatformAbbreviation) AS sResult, games.GameID
        FROM
            games
            Inner Join platforms ON games.PlatformID = platforms.PlatformID
            Inner Join titles ON titles.TitleID = games.TitleID
        WHERE
            UCase(titleName) LIKE Ucase('#ARGUMENTS.SearchString#%')
        ORDER BY
            titleName ASC;
    </cfquery>

Two things: First of all, I would like to get the GameID back to the page making the AJAX request; I know why it is not coming back: Because I'm only returning sResult var, which does not include the GameID. Is there a way to return the GameID value without displaying it?

The second thing: How to I grab a value from the auto-suggest once it is returned? Say I want to grab the GameID, or if I can't do that, the "TitleName" to use that in my query?

I tried passing it to the form this way: action="search.cfm?GameID=#cfautosuggestvalue.GameID#" - but that does not work. How do I reference the autosuggestionvalue varaibles for use?

Thanks

+1  A: 

Unfortunately, you can only return a list/array of simple values. You can't return both a name and another key. There are workarounds. If your names are unique, you can look that up on the server (ie, convert "Raymond" to 1), but that only works for unique names. I've got a blog entry that goes into detail here:

http://www.coldfusionjedi.com/index.cfm/2009/11/22/Ask-a-Jedi-ColdFusion-Autosuggest-on-2-Columns

CF Jedi Master
Thanks Ray... any ideas though, why the form is only taking in my keyboard input and not the auto-suggest value? For example, if I type "m" and a "metal gear" shows up, clicking on will take me to search.cfm, but when I dump #form# I get "m" and not "metal gear"--why is that?
Mel
If you bind to onClick, it'll send 'm'. If you bind to onChange, it should send the last changed value in the input field.
Henry
+1  A: 

If you were using either afterUpdateElement in prototype/scriptaculous or the result() function in JQuery this would be possible and a lot more straightforward.

The following examples are each taken from their respective sites

prototype/scriptaculous:

<ul>
    <li id="1">your mom</li>
    <li id="2">yodel</li>
</ul>

new Ajax.Autocompleter("autocomplete", "autocomplete_choices", "/url/on/server", {
  afterUpdateElement : getSelectionId
});

function getSelectionId(text, li) {
    alert (li.id);
}

JQuery

var data = [ {text:'Link A', url:'/page1'}, {text:'Link B', url: '/page2'} ];
$("...").autocomplete(data, {
  formatItem: function(item) {
    return item.text;
  }
}).result(function(event, item) {
  location.href = item.url;
});
muloka