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)
{
xmlhttp=new XMLHttpRequest();
}
else
{
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 " , .
,