runeveryday
runeveryday

Reputation: 2799

google custom search can't work

Here is the test.html file code:

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

I put the code which generated by the http://www.google.com/cse/manage/create?hl=en. The "Sites to search" I entered that was http://stackoverflow.com. When I put the generated code into the cse.php. Then put the cse.php and test.html into my local php enviroment. When I entered the text "php" into the search textbox and click the search button. But there is no any result on my search result page. What's wrong with my steps and code? thank you.

Here is the cse.php file code:

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

Upvotes: 2

Views: 1173

Answers (2)

Marcel
Marcel

Reputation: 28087

I'm not sure where to start with your code, but with a fresh code snippet from the Custom Search code generator, you can pass a query string to the CustomSearchControl. With code borrowed from here, something like this should work:

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

So, for example that would go in your cse.php page (although there's no PHP in there at this stage) and your initial page's form something like:

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

Upvotes: 1

Winston Ewert
Winston Ewert

Reputation: 45039

Get rid of the http:// in the sites to search bit.

Upvotes: 0

Related Questions