Solved

ajax google suggest some training code for the ajax expert

Posted on 2010-11-26
2
532 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 count occurrences of each item in an array.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now