webdevRefinery Forum: Passing queries to google custom search engine - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Rate Topic: -----

User is offline Slad 

  • Group: Members
  • Posts: 33
  • Joined: 15-April 11
  • LocationGermany
  • Expertise:HTML,CSS

Posted 28 June 2012 - 04:04 PM (#1)

Passing queries to google custom search engine


So I'm trying to get a Google custom search engine working on my new site.

This is how it should work: On every page the user has a search box in the header. When he enters a query there it should be passed to searchresults.html that holds the following javascript code that I got from Google:
<div id="cse" style="width: 100%;">Loading</div>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript"> 
  google.load('search', '1', {language : 'de', style : google.loader.themes.V2_DEFAULT});
  google.setOnLoadCallback(function() {
    var customSearchOptions = {};  var customSearchControl = new google.search.CustomSearchControl(
      '001133520264490576315:dwdiy8xzoak', customSearchOptions);
    customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
    var options = new google.search.DrawOptions();
    options.enableSearchResultsOnly(); 
    customSearchControl.draw('cse', options);
    function parseParamsFromUrl() {
      var params = {};
      var parts = window.location.search.substr(1).split('\x26');
      for (var i = 0; i < parts.length; i++) {
        var keyValuePair = parts[i].split('=');
        var key = decodeURIComponent(keyValuePair[0]);
        params[key] = keyValuePair[1] ?
            decodeURIComponent(keyValuePair[1].replace(/\+/g, ' ')) :
            keyValuePair[1];
      }
      return params;
    }

    var urlParams = parseParamsFromUrl();
    var queryParamName = "q";
    if (urlParams[queryParamName]) {
      customSearchControl.execute(urlParams[queryParamName]);
    }
  }, true);
</script>
 


The search boxes on all the other pages look like this for now:
<form action="search.php" method="post" name="search" id="search">
          <img id="imageglass" src="img/search_icon.jpg"/>
           	<input type="text" name="q" id="searchfield" maxlength="255" value="" />
            <input name="imagesearch" type="image" id="imagesearch" src="img/search_go.jpg" alt="submit" />
         
          </form>


So the question here is... How do I correctly pass the required string from the search pages to the result page with the google code?

I have tried to find information on this on Google, but didn't find any suitable answer for my setup.
I hope that you guys can help me out here.
Thanks in advance for your support on this.
If it is not clear from my description what is going on, please tell me, I will try to express it differently...

PS. Sorry if this is in the wrong section... the Google code is javascript so I thought I'd put it here...


::::::: E D I T ::::::::

Hmm... So I guess it was because of the form method="post" on the search site...

Changed that to method="get" and it seems to work...

Sorry for that stupid question then. :mellow:
0


User is offline Mazc 

  • Group: Members
  • Posts: 27
  • Joined: 18-June 12
  • Expertise:HTML,CSS,PHP,SQL

Posted 29 June 2012 - 06:26 AM (#2)

Oh, I hate when you think you have tried everything and then you take time to put up a thread about how you don't understand why it doesn't work and what you've tried so far. And then approximately within 5 minutes you realize, I probably should try that and it indeed does work. :blink:
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services