是否可以将搜索变量传递到我在网站上嵌入的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>
由于
答案 0 :(得分:6)
抱歉,我知道这是一个糟糕的答案,但除了引用错误的变量名之外,你实际上已经做对了。哦,另外,作为一个旁边,我也希望你在$ q上做某种消毒,万一有人在你的表格上贴了这样的东西:term“); alert(”啊哈!
customSearchControl.draw('cse');
searchControl.execute("$q");
应该是:
customSearchControl.draw('cse');
customSearchControl.execute("$q");
另外,谢谢你的问题 - 我一直在寻找如何做到这一点!
答案 1 :(得分: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 );?>");
答案 2 :(得分:2)
如果有人正在寻找更直接/简单的解决方案。您所要做的就是将搜索关键字传递到名为 q 的GET参数(从您的自定义表单到您的GCS所在的页面),GCS将自动使用该搜索短语。
来源:https://developers.google.com/custom-search/json-api/v1/using_rest