2010-02-19 112 views
6

是否可以將搜索變量傳遞到我已嵌入到我的網站上的Google自定義搜索引擎?我可以讓搜索引擎工作,但我無法通過POST(它來自網站其他頁面上的搜索按鈕)傳遞一個字詞將變量傳遞給Google自定義搜索引擎

我試圖破解我在此處找到的代碼:http://code.google.com/apis/ajax/playground/?exp=search#hello_world

而且這是我迄今爲止...($ q是我傳遞給它的期限)

<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> 

感謝

回答

5

對不起,我知道這是一個糟糕的答案,但你」實際上它已經從引用錯誤的變量名開始。哦也,順便說一句,我也希望你做某種類型的上$ Q禁制的,萬一有人貼出這樣的事情到您的窗體:術語「);警報(」啊哈

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

應該是:

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

此外,謝謝你的問題 - 我一直在尋找如何做這自己

+0

謝謝!我一直把我的頭髮拉出來,它完美的工作。再次感謝! – Matt 2010-03-08 18:46:53

2

這是任何使用PHP試圖完成這個相同的目標,以幫助上面的例子!用途...

customSearchControl.execute("$q"); 

讀取參數是在通行證。在您將使用PHP的網站...

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

你可以使用$ _GET或$ _REQUEST,如果你的參數是不是在後。

當然,你應該先清理輸入。這樣的事情是非常弱的,但它是一個開始......

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

萬一有人找了一下更直接/簡單的解決方案。您只需將搜索關鍵字傳入名爲q的GET參數(從您的自定義表單到您的GCS所在的頁面),GCS將自動使用該搜索詞組。

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

+0

多麼簡單的解決方案。 「site.com/search?q = query''會自動搜索該查詢。謝謝! – 2016-05-21 17:10:27