views:

347

answers:

1

Is it possible to pass a search variable into the Google Custom Search Engine that I have embedded on my website? I can get the search engine to work, but I can't pass it a term via POST (it's coming from a search button on other pages of the website)

I tried to hack the code I found here: http://code.google.com/apis/ajax/playground/?exp=search#hello_world

And this is what I have so far... ($q is the term I am passing to it)

<script type="text/javascript">
    google.load('search', '1', {language : 'en'});

    function OnLoad()
    {
        var customSearchControl = new google.search.CustomSearchControl('***my key****');
        customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
        customSearchControl.draw('cse');
        searchControl.execute("$q");
    }
    google.setOnLoadCallback(OnLoad);
</script>   

Thanks

+2  A: 

Sorry, I know it's a crappy answer, but you've actually got it right apart from referencing the wrong variable name. Oh, also, as an aside, I would also hope you're doing some kind of sanitisation on $q, in case someone posted something like this to your form: term"); alert("aha!

    customSearchControl.draw('cse');
    searchControl.execute("$q");

should be:

    customSearchControl.draw('cse');
    customSearchControl.execute("$q");

Also, thank you for the question - I was looking for how to do this myself!

Shabbyrobe
Thank you!! I've been pulling my hair out over this, and it worked perfectly. Thanks again!
Matt