ajax google suggest some training code for the ajax expert

Posted on 2010-11-26
Medium Priority
Last Modified: 2012-05-10
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 ********************
$filehandle = fopen("http://www.google.com/complete/search?hl=en&js=true&qu=" .
$_GET["qu"], "r");
while (!feof($filehandle)){
      $download = fgets($filehandle);
      echo $download;

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

<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 =
                  if (XMLHttpRequestObject.readyState == 4 &&
                  XMLHttpRequestObject.status == 200) {
                        var text = XMLHttpRequestObject.responseText.replace(
                        "window.google.ac.Suggest_apply", "callback");
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');

      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;
<H1>Handling Google Suggest</H1>
Search forc <input id = "textField" type = "text"
name = "textField" onkeyup = "askGoogleSuggest()">
<div id = "targetDiv"><div></div></div>
Question by:sydneyguy

Accepted Solution

roynaufal earned 2000 total points
ID: 34221514
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:

<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) {
	url: dataSource,
	type: 'GET',
	success: function(response, textStatus, XMLHttpRequest) {
	error: function(XMLHttpRequest, textStatus, errorThrown) {
               alert('could not get data from google');


Open in new window


i think this wil make you find the problem easier

Author Closing Comment

ID: 34398819
thanks for the help am working through the solution

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question