Solved

ajax google suggest some training code for the ajax expert

Posted on 2010-11-26
2
536 Views
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 ********************
<?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
Comment
Question by:sydneyguy
2 Comments
 
LVL 3

Accepted Solution

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

Author Closing Comment

by:sydneyguy
ID: 34398819
thanks for the help am working through the solution
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

785 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