msaz87 Posted July 21, 2011 Share Posted July 21, 2011 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 == 'Search') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search';}" /> <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 Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 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. Quote Link to comment Share on other sites More sharing options...
msaz87 Posted July 27, 2011 Author Share Posted July 27, 2011 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.