Google live search - Connecting to Google Suggest - shows the whole code to help you ~ Ajax Training, Learn Ajax Video Tutorials Online, Ajax Examples
Ajax Training, Learn Ajax Video Tutorials Online, Ajax Examples: Google live search - Connecting to Google Suggest - shows the whole code to help you

Google live search - Connecting to Google Suggest - shows the whole code to help you

<html>
<head>
<title>Google live search</title>
<style>
#targetDiv {
background-color: #FFEEAA;
width: 30%;
}
</style>
<script language = “javascript”>
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject(“Microsoft.XMLHttp”);
}
function getData(dataSource)
{
if(XMLHttpRequestObject) {
XMLHttpRequestObject.open(“GET”, dataSource);
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
eval(XMLHttpRequestObject.responseText);
}
}
XMLHttpRequestObject.send(null);
}
}
function getSuggest(keyEvent)
{
keyEvent = (keyEvent) ? keyEvent: window.event;
input = (keyEvent.target) ? keyEvent.target :
keyEvent.srcElement;
if (keyEvent.type == “keyup”) {
if (input.value) {
getData(“google.php?qu=” +
input.value);
}
else {
var targetDiv = document.getElementById(“targetDiv”);
targetDiv.innerHTML = “<div></div>”;
}
}
}
function sendRPCDone(unusedVariable, searchTerm, arrayTerm,
arrayResults, unusedArray)
{
var data = “<table>”;
var loopIndex;
if (arrayResults.length != 0) {
for (var loopIndex = 0; loopIndex < arrayResults.length;
loopIndex++) {
data += “<tr><td>” +
“<a href=’http://www.google.com/search?q=” +
arrayTerm[loopIndex] + “‘>” + arrayTerm[loopIndex] +
‘</a></td><td>’ + arrayResults[loopIndex] + “</td></tr>”;
}
}
data += “</table>”;
var targetDiv = document.getElementById(“targetDiv”);
targetDiv.innerHTML = data;
}
</script>
</head>
<body>
<H1>Google live search</H1>
Search for <input id = “textField” type = “text” name = “textField” onkeyup = “getSuggest(event)”>
<div id = “targetDiv”><div></div></div>
</body>
</html>

Check out the PHP script, google.php, which is the script that actually does the communicating with Google. This one takes a little PHP of the kind that appears in detail in Chapter 10. This script is passed the term the user has entered into the text field, and it should get some suggestions from Google, which it does like this with the PHP fopen (file open) statement:

<?php
$handle = fopen(“http://www.google.com/complete/search?hl=en&js=true&qu=” .
$_GET[“qu”], “r”);

.
.
.


This gives you a PHP file handle, which you can use in PHP to read from the Google URL. Here’s how that looks in PHP, where a while loop keeps reading data from Google as long as the end of the data marker isn’t seen. You can check if you’ve reached the end of the data with the feof function, which returns true if the end of the data has been reached:

<?php
$handle = fopen(“http://www.google.com/complete/search?hl=en&js=true&qu=” .
$_GET[“qu”], “r”);
while (!feof($handle)){
.
.
.
}
?>

To get the data from Google, you can use the fgets (file get string) function, and echo the fetched text, which sends that text back to the browser. Here’s how you can make that happen:

<?php
$handle = fopen(“http://www.google.com/complete/search?hl=en&js=true&qu=” .
$_GET[“qu”], “r”);
while (!feof($handle)){
$text = fgets($handle);
echo $text;

}
fclose($handle);
?>

And that’s all you need. Now this script, google.php, will read the suggestion data from Google and send it back to your script.

Everything works as expected.

Related Posts by Categories

0 comments:

Useful Links on Adobe Flex

Your Ad Here

Latest Books on Adobe Flex