views:

813

answers:

4

Hi!

I added Google and Wikipedia search box to a page and I'm looking for a way to change the suggestions depending on the selected radio button.

In fact the suggestions language should change depending on the language checked.

Here is my actual code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>Google Suggest Test</title>
 <script type="text/javascript" src="googlesuggest.js"></script>
</head>
<body>
<form action="http://www.google.com/search" id="google_suggest" >
 <div>
  <input name="q" value="" type="text" />
  <input type="submit" value="OK" />
  <input type="hidden" name="complete" value="1" />
  <input type="hidden" name="ie" value="UTF-8" />
  <input type="hidden" name="oe" value="UTF-8" />
  <input type="hidden" name="num" value="30" />
  <input type="hidden" name="channel" value="s" />
  <br />
  <label for="all"><input id="all" type="radio" name="lr" value="" checked="checked" />All</label>
  <label for="lang_en"><input id="lang_en" type="radio" name="lr" value="lang_en" />English</label>
  <label for="lang_fr"><input id="lang_fr" type="radio" name="lr" value="lang_fr" />French</label>
  <label for="lang_de"><input id="lang_de" type="radio" name="lr" value="lang_de" />German</label>
  <label for="lang_ru"><input id="lang_ru" type="radio" name="lr" value="lang_ru" />Russian</label> 
  <label for="lang_es"><input id="lang_es" type="radio" name="lr" value="lang_es" />Spanish</label>
  <br /><br />
 </div>
</form>
<form method="post" action="wikipedia_action.php">
 <div>
  <input type="text" name="wikipedia_field" />
  <input type="submit" value="OK" />
  <br />
  <label for="wik_en"><input type="radio" checked="checked" name="wikipedia" id="wik_en" value="wik_en" />English</label>
  <label for="wik_fr"><input type="radio" name="wikipedia" id="wik_fr" value="wik_fr" />French</label>
  <label for="wik_de"><input type="radio" name="wikipedia" id="wik_de" value="wik_de" />German</label>
  <label for="wik_ru"><input type="radio" name="wikipedia" id="wik_ru" value="wik_ru" />Russian</label>
  <label for="wik_es"><input type="radio" name="wikipedia" id="wik_es" value="wik_es" />Spanish</label>
 </div>
</form>
</body>
</html>

Script googlesuggest.js:

$(document).ready(function(){

window.google = {
    kEI: "wR-4SfmNIMyA-AbluKj5Cg",
    kEXPI: "17259,17291,18169",
    kHL: "fr"
};
var _gjwl = location;
function _gjuc() {
    var a = _gjwl.hash.substring(1);
    if (/(^|&)q=/.test(a) && a.indexOf("#") == -1 && !/(^|&)cad=h($|&)/.test(a)) {
        _gjwl.replace("search?" + a.replace(/(^|&)fp=[^&]*/g, "") + "&cad=h");
        return 1
    }
    return 0
};
window._gjuc && location.hash && _gjuc();

google.y = {};
google.x = function(e, g) {
    google.y[e.id] = [e, g];
    return false
};
window.clk = function(b, c, d, e, f, g, h) {
    if (document.images) {
        var a = encodeURIComponent || escape;
        (new Image).src = ["/url?sa=T", c ? "&oi=" + a(c) : "", d ? "&cad=" + a(d) : "", "&ct=", a(e), "&cd=", a(f), b ? "&url=" + a(b.replace(/#.*/, "")).replace(/\+/g, "%2B") : "", "&ei=wR-4SfmNIMyA-AbluKj5Cg", g].join("")
    }
    return true
};
window.gbar = {
    qs: function() {},
    tg: function(e) {
        var o = {
            id: 'gbar'
        };
        for (i in e) o[i] = e[i];
        google.x(o,
        function() {
            gbar.tg(o)
        })
    }
};

if (google.y) google.y.first = [];
window.setTimeout(function() {
    var xjs = document.createElement('script');
    xjs.src = 'http://www.google.com/extern_js/f/CgJmciswCjgNLCswDjgELCswFjgELCswFzgBLCswGDgDLCswJTjJiAEsKzAnOAAs/4Pb8ykSjyRQ.js';
    document.getElementsByTagName('head')[0].appendChild(xjs)
},
0);
google.y.first.push(
    function(){
        var google_suggest = document.getElementById('google_suggest');
        google.ac.i(
            google_suggest,
            google_suggest.q,
            '',
            ''
        )
    }
)
function _gjp() { ! (location.hash && _gjuc()) && setTimeout(_gjp, 500);
}
window._gjuc && _gjp();

});

PHP of wikipedia_action.php

<?php
$search_field = trim($_POST['wikipedia_field']);
$search_engine = trim($_POST['wikipedia']);
$url_params = preg_replace('/(\ )+/', '+', $search_field);
$url = array('wik_en'=>'http://en.wikipedia.org/wiki/Special:Search?search=', 'wik_fr'=>'http://fr.wikipedia.org/wiki/Special:Search?search=', 'wik_de'=>'http://de.wikipedia.org/wiki/Special:Search?search=', 'wik_ru'=>'http://ru.wikipedia.org/wiki/Special:Search?search=', 'wik_es'=>'http://es.wikipedia.org/wiki/Special:Search?search=');
header('Location:'.$url[$_POST['wikipedia']].$url_params)
?>

Actually I have 2 problems…

1 - I noticed for Google suggestions that changing kHL:"en" into kHL:"fr" for French or kHL:"de" for German does the trick but I don't know how to change it depending on radio button checked? (and without some onClick, jQuery should be better)

2 - I also failed to set up Wikipedia suggestion system that's why I'm looking for this (and suggestions change) too. It's used here:

http://en.wikipedia.org/wiki/Special:Search

http://fr.wikipedia.org/wiki/Sp%C3%A9cial:Recherche

http://de.wikipedia.org/wiki/Spezial:Suche

Etc.

Any help would be appreciated ;)

A: 

I need a tree stump pulled in my back yard, come over and dig it out and I'll code this up for ya ;)

Chad Grant
A: 

I think you want to do something on the change event in jquery.

http://docs.jquery.com/Events/change

In the example on that page, you would change the select to radio. Anyway, check out the examples on that page and it should point you in the right direction

jimiyash
A: 

Do you have the two scripts working independently? In other words, one page with Google suggest and one with WP suggest? If so, I would recommend simply using two forms, with unique IDs, allowing each one to work separately.

Hide the one you're not using. When you click the radio button to switch 'em, hide one form and display the other.

DisgruntledGoat
I have 1 form for Google and an other for Wikipedia but both on the same page. But for each form I would like to change script depending on language selected.
Mark
Sorry, I misread your question and though the radio button was to switch between Google or Wikipedia. Disregard.
DisgruntledGoat
A: 

This post should really be broken down into 2 separate questions. As far as the Google Suggest part of your question. You can the following bit of jquery code to change the kHL when clicking on the radio button:

<script type="text/javascript">
   $(function(){
     $("#google_suggest input[type='radio']").click(function(){
        alert("kHL Before="+window.google.kHL);
        window.google.kHL = this.id.replace('lang_',''); 
        alert("kHL After="+window.google.kHL);
     });   
   });
 </script>

However, while you can tell by the alerts that the kHL changes correctly, the GoogleSuggest dropdown does not change the language. In other words, the kHL is stuck to what was setup inside $(document).ready().

The only thing that I can think of is reloading the googlesuggest.js dynamically with the onchange() event. This is something you will need to investigate further.

ichiban
You're right I'll seperate this post in 2, or edit it once I'll find out about Wikipedia system and remove my question about it.I'll look into it, try your code and suggestions and let you know.
Mark