开发者

How do I grab and use a variables coming back through ValueList from an AJAX call?

开发者 https://www.devze.com 2022-12-22 20:04 出处:网络
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

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 sR开发者_StackOverflow社区esult 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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消