hi guys, could you help me with this one?
this is about the autocomplete in php using a textbox..
my problem with this code is that when i used KeyDown on my keyboard,
it should automatically input in the textbox the data, but mine is different,
it displays "undefined" instead. same thing goes whenever i click the data using a mouse..
please help/

****this is for the index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<script type="text/javascript" src="jsfile.js"></script>
<script type="text/javascript" >
	function setValue(str)
	{
		document.getElementById("name").value=str;
	}
</script>
 <BODY>
Name:
<!--<input style="width: 200px;" id="name" onKeyUp="getInfo(this.value,200)" type="text" />
<div id="autoSuggestionsList" style="position: relative; top: -4px; width='100px;'" ></div>
!-->
<input style="width: 200px;" id="name" onKeyUp="getInfo(this.value)" type="text" />
<div id="autoSuggestionsList"></div>
  
 </BODY>
</HTML>

*****this one is my javascript - jsfile.js

var httpobject;

function getInfo(str,length)
{
	if(str.length != 0)
	{
		httpobject=GetHttpObject();
		
		if (httpobject !=null)
  		{
			var url="autocomplete.php";
			url=url+"?str="+str;
			url=url+"&length="+length;
			httpobject.onreadystatechange=stateChanged;
			httpobject.open("GET",url,true);
			httpobject.send(null);
 		}
 	}
	else
	{
		document.getElementById("autoSuggestionsList").innerHTML="";
	}
}

function stateChanged()
{
	if (httpobject.readyState==4)
	{
		document.getElementById("autoSuggestionsList").innerHTML=httpobject.responseText;
	}
}

function GetHttpObject()
{
	if (window.ActiveXObject) 
		return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) 
		return new XMLHttpRequest();
  	else 
  	{
  		alert("Your browser does not support AJAX.");
		return null;
  	}
}

i don't think i will put anymore my .php file since my problem deals with the javascript code..

thanks a lot ^_^

Recommended Answers

All 2 Replies

Have you tried using jQueryUI? It already has the functionality you need
http://jqueryui.com/demos/autocomplete/#remote

But the problem could be that you define a function with two input variables but only pass one to it when you run it
Maybe change

<input style="width: 200px;" id="name" onKeyUp="getInfo(this.value)" type="text" />
<div id="autoSuggestionsList"></div>

to

<input style="width: 200px;" id="name" onKeyUp="getInfo(this.value, this.value.length)" type="text" />
<div id="autoSuggestionsList"></div>

still sir, the code does not work T_T

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.