Real time help in Python / Javascript / AJAX

I am trying to create a live AJAX search (e.g. Google Suggest) using python. I am new to AJAX, so I started reading some tutorials and other useful documentation. I found an example http://www.w3schools.com/ajax/ajax_aspphp.asp , which is basically what I'm trying to execute, but this example only applies to asp / php. So far I have managed to encode part of the python program, however, I am getting an undefined error.

Below is the javascript code (test.js, which is basically the same as in the example):

function showHint(str)
{
var xmlhttp;
if (str.length==0)
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","search.py?q="+str,true);
xmlhttp.send();
}

In firefox 4, the error "str is undefined" appears.

Below is my html part of my Python code:

<html>
    <head>
        <title>Live Search</title>
        <script src="http://localhost:8000/test.js" type="text/javascript"></script>
    </head>
    <body>
        <h1> Cities </h1>
        Enter Anything: <input type="text" id="sname" onkeyup="showHint(this.text)">
        %(search)s
    </body>
</html>

: , onkeyup = "showHint (this.id), - .. " sname " , .

,

+3
1

<input> . this.text, str undefined. , <input type="text" id="sname" onkeyup="showHint(this.value)"> , .

+2

All Articles