将变量传递给Google自定义搜索引擎

6

我是否可以将搜索变量传递给我嵌入网站的Google自定义搜索引擎?我可以让搜索引擎工作,但无法通过POST传递术语(它来自网站其他页面上的搜索按钮)

我尝试了在此处找到的代码: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>   

谢谢

3个回答

6

抱歉,我的答案可能不太好,但你除了引用错误的变量名外,其他都是正确的。另外,我也希望你对 $q 进行一些清理工作,以防万一有人向你的表单中发布了这样的内容:term"); alert("aha!

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

should be:

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

另外,感谢您的提问 - 我也在寻找如何自己完成这个操作!


谢谢!我为此苦恼了很久,现在它完美地运行了。再次感谢! - Matt

2

这是为了帮助使用PHP的人们实现同样的目标。上面的例子使用...

customSearchControl.execute("$q");

要读取传递的参数。在PHP网站上,您可以使用...

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

如果您的参数不在POST中,可以使用$_GET或$_REQUEST。

当然,您应该先对输入进行过滤。以下是一个相当简单的示例,但这只是一个开始...

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

2

多么美妙而简单的解决方案啊。site.com/search?q=query将自动使用该查询进行搜索。谢谢! - Alex Johnson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接