Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ajax google suggest some training code for the ajax expert

Posted on 2010-11-26
2
Medium Priority
?
548 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
roynaufal earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

618 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