Domanda

I am pretty good with Coldfusion and passing variables in forms through the web via URL. I cannot wrap my head around this mobile stuff yet. I am developing an app that pulls from a database on my server. I have 2 calls to the server now that just pulls data without any "where" statement and they work great. I want to add a search input that will have what the user types in the box to go in my query on my .cfc. Not sure how to pass that data from the phone form to the cfc on my server.

Here's the search button code...

<form action="searchresult.html" method="post"  data-transition="none">
    <input type="search" name="mySearch" 
           id="mySearch" value="" data-mini="true" data-theme="b" />
</form>

This is my script code in XCode that should run when the search is submitted...(i don't know where to put any variable to pass to the cfc. Can it be passed in the URL?)

$("#resultPage").live("pageshow", function() {
    console.log("Getting remote list" + event.notification);
    $.mobile.showPageLoadingMsg();
    $.get("http://www.mywebsite.com/jquery/ryprad.cfc?
                      method=getsearch&returnformat=json", 
        {}, 
        function(res) {
            $.mobile.hidePageLoadingMsg();
            var s = "";
            for(var i=0; i<res.length; i++) {
                s+= "<li><a name=" + res[i].id + " + href='" 
                    + res[i].showlink + "'>" 
                    + res[i].date + "<br/>" + res[i].name + "<br/>" 
                    + res[i].description + "</a></li>";
            }

            $("#resultList").html(s);
            $("#resultList").listview("refresh");
            },
        "json"
    );
});

And this is my cfc on the server...

component {
remote array function getsearch() {
    var q = new com.adobe.coldfusion.query();
        q.setDatasource("myDat");
        q.setSQL("
            select id1, Date, ShowLInk, IntName, description from RYPRadio 
            Where intName 
            LIKE '%#VARIABLE_FROM_PHONE_SEARCH#%' 
            order by date desc"
        );
        var data = q.execute().getResult();
        var result = [];
        for(var i=1; i<= data.recordCount; i++) {
            arrayAppend(
                result, 
                {
                    "id"=data.id1[i], 
                    "name"=data.IntName[i], 
                    "date"=dateformat(data.date[i], "mmmm d, yyyy"), 
                    "description"=data.description[i], 
                    "showlink"=data.ShowLInk[i]
                }
            );
        }
        return result;
    }
}

I hope someone can help with this. If I can get this working, it will help me develop other things that will pass variables such as this!

È stato utile?

Soluzione

Add the search string to the jquery call:

$.get("http://www.mywebsite.com/jquery/ryprad.cfc?method=getsearch&returnformat=json&searchName="+ $("#mySearch").val(), {}, function(res) {

And add an argument in ColdFusion:

remote array function getsearch( searchName ) {

And adjust your query accordingly. You should use a param for this like so:

q.setSQL("select id1, Date, ShowLInk, IntName, description from RYPRadio Where intName LIKE :searchParam order by date desc");
q.addParam( name="searchParam", value="%#searchName#%" );
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top