Jump to content

Google Search API Form Receipt Issue


msaz87

Recommended Posts

Hey all,

 

I'm pretty sure this is a very easy solution, I've just been staring at it too long and am probably missing the cause.

 

I'm using Google's site search API and using their "Results Only" setup, so I design my own search box, then just drop their code into the results page and specify the variable the query is being passed as. So here's my search box:

 

				<form method="post" action="/search-results/?q=">    	
<input id="searchTxt" type="text" maxlength="128" name="q" size="15" value="Search" onfocus="if (this.value == &#039;Search&#039;) {this.value = &#039;&#039;;}" onblur="if (this.value == &#039;&#039;) {this.value = &#039;Search&#039;;}" />
				<input type="image" name="op" value="Search" src="/wp-content/uploads/2011/07/search-button-magnifier.png" />

 

And the results code via Google:

<div id="cse" style="width: 100%;">Loading</div>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript"> 
  function parseQueryFromUrl () {
    var queryParamName = "q";
    var search = window.location.search.substr(1);
    var parts = search.split('&');
    for (var i = 0; i < parts.length; i++) {
      var keyvaluepair = parts[i].split('=');
      if (decodeURIComponent(keyvaluepair[0]) == queryParamName) {
        return decodeURIComponent(keyvaluepair[1].replace(/\+/g, ' '));
      }
    }
    return '';
  }
  google.load('search', '1', {language : 'en'});
  google.setOnLoadCallback(function() {
    var customSearchControl = new google.search.CustomSearchControl('XXXXXXXXXXXXXXXXXXX');
    customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
    var options = new google.search.DrawOptions();
    options.enableSearchResultsOnly();     
    customSearchControl.draw('cse', options);
    var queryFromUrl = parseQueryFromUrl();
    if (queryFromUrl) {
      customSearchControl.execute(queryFromUrl);
    }
  }, true);
</script>
<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" /> 

 

But nothing seemingly the query is not sent with the user to the results page. Manually adding "?q=test" shows the results work. Am I missing something here? I'm using Wordpress on this site, so not sure if the permalink setup is screwing with it at all.

 

Thanks for the help

Link to comment
Share on other sites

if you want the value of "q" to be present in the query string, your form should use method="get" instead of method="post".

 

change the form line from this

<form method="post" action="/search-results/?q=">

to this

<form method="get" action="/search-results/">

and try again. ?q= will be automatically added to the url, so I removed it from the action="" parameter.

Link to comment
Share on other sites

That was what was wrong... only I fixed it in a much more difficult way. I realized the problem was that the query had to be actually in the URL, so I had the form post to a simple redirect page that then rebuilt the search link with the query in it.

 

The whole _GET, _POST, _REQUEST thing is still a little foreign to me, but your solution definitely makes sense and seems to be the way to go. Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.