Note that there are some explanatory texts on larger screens.

plurals
  1. POPython/Javascript/AJAX live search help
    primarykey
    data
    text
    <p>I'm trying to create an AJAX live search (such as Google Suggest) with python. I'm new to AJAX, so I started reading some tutorials and other useful documentation. I found an example, <a href="http://www.w3schools.com/ajax/ajax_aspphp.asp" rel="nofollow">http://www.w3schools.com/ajax/ajax_aspphp.asp</a>, which is basically what I'm trying to accomplish but this example is for asp/php only. I managed so far to code the python portion of the program, however I'm getting an undefined error.</p> <p>Below is my javascript code (test.js, which is basically the same as the one from the example):</p> <pre><code>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 &amp;&amp; xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","search.py?q="+str,true); xmlhttp.send(); } </code></pre> <p>In firefox 4, it comes up with the error "str is undefined".</p> <p>Below is my html portion of my python code:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Live Search&lt;/title&gt; &lt;script src="http://localhost:8000/test.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt; Cities &lt;/h1&gt; Enter Anything: &lt;input type="text" id="sname" onkeyup="showHint(this.text)"&gt; %(search)s &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Note: before I had onkeyup="showHint(this.id) but it doesn't work for some reason.. it keeps passing "sname" as the query instead of what I type into the input field.</p> <p>Please help</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload