Solved

ajax google suggest some training code for the ajax expert

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How can I do this in Pyhton? 12 74
Re-imbursement Claim System 3 25
whm high memory usage in processes 7 38
Not needed 13 58
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, …
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

747 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

12 Experts available now in Live!

Get 1:1 Help Now