[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 550
  • Last Modified:

ajax google suggest some training code for the ajax expert

an easy one for a guru of ajax
am running through some traing code examples and have this one that access the suggest tool have included the code below, now am not sure if this area still operates as i rember seeing some were the they were or had stopped certain areas not sure if this is one or whether there is a bug in the code, if you could have a look it would be appreciated


************  called   google.php ********************
<?php
$filehandle = fopen("http://www.google.com/complete/search?hl=en&js=true&qu=" .
$_GET["qu"], "r");
while (!feof($filehandle)){
      $download = fgets($filehandle);
      echo $download;
}
fclose($filehandle);
?>


*******  called google.html ******************
<html>
<head>
<title>Handling Google Suggest</title>
<style>
#targetDiv {
background-color: #FFAAAA;
width: 40%;
}

</style>
<script language = "javascript">
function getData(dataSource)
{
      
      var XMLHttpRequestObject = false;
      if (window.XMLHttpRequest) {
            XMLHttpRequestObject = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
            XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
      }
      if(XMLHttpRequestObject) {
            XMLHttpRequestObject.open("GET", dataSource);
            
            XMLHttpRequestObject.onreadystatechange =
            function()
            {
                  if (XMLHttpRequestObject.readyState == 4 &&
                  XMLHttpRequestObject.status == 200) {
                        
                        var text = XMLHttpRequestObject.responseText.replace(
                        "window.google.ac.Suggest_apply", "callback");
                        eval(text);
                  }
            }
            
            XMLHttpRequestObject.send(null);
            
      }
}
function askGoogleSuggest()
{
      
      var input = document.getElementById("textField");
      if (input.value) {
            
            getData("google.php?qu=" + input.value);
            
      }
      else {
            var targetDiv = document.getElementById("targetDiv");
            targetDiv.innerHTML = "<div></div>";
      }
      
}
function callback(unusedVariable, searchTerm, arrayTerm,unusedArray)
{
      var data = "<table>";
      var loopIndex;
      alert( 'dataSource');
      alert(unusedVariable);

      for (loopIndex = 1; loopIndex < arrayTerm.length;
            loopIndex++) {
            data += "<tr><td>" +
            "<a href='http://www.google.com/search?q=" +
            arrayTerm[loopIndex] + "'>" + arrayTerm[loopIndex] +
            '</a></td><td>' + arrayTerm[++loopIndex] + "</td></tr>";
      }
      data += "</table>";
      var targetDiv = document.getElementById("targetDiv");
      targetDiv.innerHTML = data;
}
</script>
</head>
<body>
<H1>Handling Google Suggest</H1>
Search forc <input id = "textField" type = "text"
name = "textField" onkeyup = "askGoogleSuggest()">
<div id = "targetDiv"><div></div></div>
</body>
</html>
googlesugest.jpg
0
sydneyguy
Asked:
sydneyguy
1 Solution
 
roynaufalCommented:
hmmmm
first off I would suggest using jquery for the AJAX, it makes your life much much easier + it will probably help you debug the problem easier....if you can do that as step 1 and check into it and post your feedback, it would be great...I'm kinda in a rush now to troubleshoot the above issue, but I suggest to try jquery to start with as such:

<head>
<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

then you can do an ajax function like this:
function getData(dataSource) {
  $.ajax({
	url: dataSource,
	type: 'GET',
	success: function(response, textStatus, XMLHttpRequest) {
                         yourcallbackfunction();
	},
	error: function(XMLHttpRequest, textStatus, errorThrown) {
               alert('could not get data from google');
	} 
  });


}

Open in new window


</head>


i think this wil make you find the problem easier
0
 
sydneyguyAuthor Commented:
thanks for the help am working through the solution
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now