Pass variable to Google Custom Search Engine

Can I pass a search variable to the Google Custom Search Engine that I embedded on my website? I can make the search engine work, but I can not pass the term to it through POST (it comes from the search button on other pages of the website)

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

And this is what I still have ... ($ q is the term I pass 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

+6
search-engine google-cse
source share
3 answers

Sorry, I know this is a crappy answer, but you really understood it without referring to the wrong variable name. Oh, as well as on the sidelines, I also hope that you will make some sort of reorganization on $ q if someone posts something like this in your form: the term "), alert (" aha!

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

it should be:

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

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

+5
source share

This will help anyone using PHP try to achieve the same goal. The above example uses ...

 customSearchControl.execute("$q"); 

to read the passed parameter. On a PHP site you will use ...

 customSearchControl.execute("<?php echo $_POST['your_paramter_name_here'];?>"); 

You can use $ _GET or $ _REQUEST if your parameter is not specified in the message.

Of course, you should first misinform the entrance. Something like this is pretty weak, but it's a start ...

 customSearchControl.execute("<?php echo htmlentities( trim( $_POST['your_paramter_name_here'] ), ENT_QUOTES );?>"); 
+2
source share

In case someone is looking for a slightly more direct / simple solution. All you have to do is pass the keywords to the GET parameter named q (from your user form to the page where your GCS is located), GCS automatically uses this search phrase.

Source: https://developers.google.com/custom-search/json-api/v1/using_rest

+1
source share

All Articles