2011-03-20 98 views
2

这里是test.html文件放在代码:谷歌定制搜索无法正常工作

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-hans" xml:lang="zh-hans"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>test</title> 
</head> 
<body> 
    <div class="fr_search">   
     <form action="cse.php" accept-charset="UTF-8" method="post" 
       id="search-theme-form">  
      <input type="text" maxlength="128" name="search_theme_form" 
       id="edit-search-theme-form-1" size="15" value="" class="form-text" /> 
      <input type="image" name="submit" id="edit-submit" 
       class="form-submit" src="images/search_btn_top.gif" /></div> 
     </form> 
    </div> 
</body> 
</html> 

我把其中由http://www.google.com/cse/manage/create?hl=en生成的代码。 “我要输入的网站”是http://stackoverflow.com。当我把生成的代码放入cse.php。然后把cse.php和test.html放到我的本地php环境中。当我将文本“php”输入到搜索文本框中并单击搜索按钮时。但我的搜索结果页面上没有任何结果。我的步骤和代码有什么问题?谢谢。

这里是cse.php文件代码:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title>test search</title> 
</head> 
<body> 
    <script src="http://www.google.com/jsapi" type="text/javascript"></script> 
    <script type="text/javascript"> 
    google.load('search', '1', {language : 'en'}); 
    google.setOnLoadCallback(function() 
    { 
     var customSearchControl = new google.search.CustomSearchControl ('011247711644571852159:xe2ytn1hwsa'); 
     customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET); 
     customSearchControl.draw('cse'); 
    }, true); 
     </script> 
     <link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" 
      type="text/css" />  
<?php echo 'test'; ?> 
</body> 
</html> 
+1

什么是PHP文件? – Blender 2011-03-20 03:33:19

+0

魔法不足。在尝试插入自己的代码之前,网站是否在Google演示中工作? – 2011-03-20 03:37:38

+0

我添加了php文件代码。 – runeveryday 2011-03-20 03:49:49

回答

0

在网站摆脱http://的搜索位。

1

我不确定从哪里开始使用您的代码,但使用Custom Search code generator的新代码段,您可以将查询字符串传递给CustomSearchControl。随着代码借来from here,这样的事情应该工作:

<div id="cse">Loading&hellip;</div> 
<script src="http://www.google.com/jsapi"></script> 
<script> 

    // Extract user's query from the URL 
    function getQuery() { 
     var url = '' + window.location; 
     var queryStart = url.indexOf('?') + 1; 
     if (queryStart > 0) { 
      var parts = url.substr(queryStart).split('&'); 
      for (var i = 0; i < parts.length; i++) { 
       if (parts[i].length > 2 && parts[i].substr(0, 2) == 'q=') { 
        return decodeURIComponent(parts[i].split('=')[1].replace(/\+/g, ' ')); 
       } 
      } 
     } 
     return ''; 
    } 

    google.load('search', '1', {language:'en' }); 
    google.setOnLoadCallback(function() { 
     var cseControl = new google.search.CustomSearchControl('ID_GOES_HERE'); 
     cseControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET); 
     cseControl.draw('cse'); 
     // Execute a query based on the query string 
     cseControl.execute(getQuery()); 
    }, true); 
</script> 

因此,例如可能会包含在您的cse.php页(虽然那里面有没有PHP在这个阶段)和你的初始页面的形式是这样的:

<form action="cse.php" method="get"> 
    <input name="q"> <input type="submit"> 
</form>