0

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 ^_^

2
Contributors
2
Replies
14
Views
6 Years
Discussion Span
Last Post by xuexue
0

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>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.