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

发布于 2024-08-22 04:46:41 字数 843 浏览 10 评论 0原文

是否可以将搜索变量传递到我嵌入网站的 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>   

谢谢

Is it possible to pass a search variable into the Google Custom Search Engine that I have embedded on my website? I can get the search engine to work, but I can't pass it a term via POST (it's coming from a search button on other pages of the website)

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

And this is what I have so far... ($q is the term I am passing 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不是你的备胎 2024-08-29 04:46:41

抱歉,我知道这是一个糟糕的答案,但除了引用错误的变量名称之外,您实际上已经得到了正确的答案。哦,另外,顺便说一句,我还希望您对 $q 进行某种清理,以防有人在您的表单中发布类似的内容:term");alert("啊哈!

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

应该是:

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

另外,谢谢你的问题 - 我一直在寻找如何自己做到这一点!

Sorry, I know it's a crappy answer, but you've actually got it right apart from referencing the wrong variable name. Oh, also, as an aside, I would also hope you're doing some kind of sanitisation on $q, in case someone posted something like this to your form: term"); alert("aha!

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

should be:

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

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

我为君王 2024-08-29 04:46:41

这是为了帮助任何使用 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 );?>");

This is to help anyone using PHP trying to accomplish this same goal. The example above uses...

customSearchControl.execute("$q");

to read the parameter being passes in. On a PHP site you would use...

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

You could use $_GET or $_REQUEST if your parameter is not in the post.

Of course you should should sanitize the input first. Something like this is pretty weak but it's a start...

customSearchControl.execute("<?php echo htmlentities( trim( $_POST['your_paramter_name_here'] ), ENT_QUOTES );?>");
分分钟 2024-08-29 04:46:41

如果有人正在寻找更直接/简单的解决方案。您所要做的就是将搜索关键字传递到名为 q 的 GET 参数(从您的自定义表单到 GCS 所在的页面),GCS 将自动使用该搜索短语。

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

In case someone is looking for a bit more straight forward / simple solution. All you have to do is to pass search keywords into GET parameter named q (from your custom form into page where your GCS is), GCS will automatically use that search phrase.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文