Autocomplete and ajax
Autocomplete and ajax
This is where I want the autocompletion.
- <td>Tilføj servicedel, varenummer:<input type="text" name="varenummer" id="varenummer" value="" onkeyup="gethint(this.value)"></td>
And here is my ajax call gethint()
- function gethint(str) {
- if (str=="") {
- document.getElementById("hint").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("hint").innerHTML=xmlhttp.responseText;
- }
- }
- xmlhttp.open("GET","gethint.php?d="+str,true);
- xmlhttp.send();
- }
And here is the gethint.php page.
- <?
- include 'include.php';
- $select2 = mysql_query("SELECT Varenum FROM Dele");
- // Fill up array with names
- while ($result = mysql_fetch_array($select2)) {
- $a[]=$result['Varenum'];
- }
- //get the q parameter from URL
- $d=$_GET["d"];
- //lookup all hints from array if length of q>0
- if (strlen($d) > 0)
- {
- $hint="";
- for($i=0; $i<count($a); $i++)
- {
- if (strtolower($d)==strtolower(substr($a[$i],0,strlen($d))))
- {
- if ($hint=="")
- {
- $hint=$a[$i];
- }
- else
- {
- $hint=$hint." <br /> ".$a[$i];
- }
- }
- }
- }
- // Set output to "no suggestion" if no hint were found
- // or to the correct values
- if ($hint == "")
- {
- $response="no suggestion";
- }
- else
- {
- $response=$hint;
- }
- //output the response
- echo $response;
- ?>
By this I can get the suggestions by writing <span id="hint"></span>
But how can I do it as a autocompletion with the jq-ui like this: autocomplete example
When $hint is a php variable and this above example is done with jq/javascript.
Any help out there?