Topic: AJAX xmlhttp.responsetext and php (Page 1 of 1) Pages that link to <a href="" title="Pages that link to Topic: AJAX xmlhttp.responsetext and php (Page 1 of 1)" rel="nofollow" >Topic: AJAX xmlhttp.responsetext and php <span class="small">(Page 1 of 1)</span>\

Bipolar (III) Inmate

Insane since: Jun 2003

IP logged posted posted 09-17-2009 02:14 Edit Quote

What i am trying to accomplish is to send an xmlhttp.request to a php page which has an array of name. if the request matches any name in the array it will return that name so i can test for equality. It works well with one name. my question is can i send back more than just one name.. like lets say a name and a message and check equality for the name and display the message. basically im asking can the xmlhttp.responsetext be used like an object with different properties so i can pull back different things to set as variables in my javascript.

my code looks like this.
1. First the html page with the form and input fields.

<script src="clienthint.js"></script>

<form name="form1">
First Name: <input type="text" id="txt1" onkeyup="showHint(this.value)" />
<p>Suggestions: <span id="txtHint"></span></p>


2. Next will be my javascript file

var xmlhttp

function showHint(str){
	if (str.length==0){
	if (xmlhttp==null){
	  alert ("Your browser does not support XMLHTTP!");
	var url="gethint.php";

function stateChanged(){
	if (xmlhttp.readyState==4){
	  if (document.getElementById("txt1").value == xmlhttp.responseText){
		document.getElementById("txt1").style.backgroundColor = "#FFFF99";  
	  else {
		document.getElementById("txt1").style.backgroundColor = "#FFFFFF";  

function GetXmlHttpObject(){
	if (window.XMLHttpRequest){
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	if (window.ActiveXObject){
	  // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	return null;

3. And last is the php page

// Fill up array with names

//get the q parameter from URL

//lookup all hints from array if length of q>0
if (strlen($q) > 0){
  	for($i=0; $i<count($a); $i++){
		if (strtolower($q)==strtolower($a[$i])){

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == ""){
	$response="no suggestion";

//output the response
echo $response;

any ideas/solutions will be greatly appreciated. Thank you in advance

Moon Shadow
Paranoid (IV) Inmate

From: Paris, France
Insane since: Jan 2003

IP logged posted posted 09-17-2009 13:10 Edit Quote

Yes, you could have your php page return an object and evaluate it into a variable, but actually you don't need to use complex objects if you're only hinting at text values.

A simpler way would be to fill the response with each successful match on a new line or separated with a custom separator. Then, write a function that takes the response text as a parameter and displays all the matched values in a convenient way that suits you. Using a <select> and wrapping the results in <option> tags is the common practice.

A few other side notes :
- Escape the $_GET parameter
- Don't forget to limit the loop on your array to a small number of successful matches.
- strtolower($q)==strtolower($a[$i]) won't get you any hint, you should not test for equality but rather if your text is contained in your value

There are lots of autocomplete plugins around these days, you might want to check this one for example :

If wishes were fishes, we'd all cast nets.

(Edited by Moon Shadow on 09-17-2009 13:11)

Bipolar (III) Inmate

Insane since: Jun 2003

IP logged posted posted 09-17-2009 15:16 Edit Quote

Thank you very much for the advice i didn't think of it that way. but im actually not trying to get a hint. I started with a hint but now i want to use it like a username availability checker. So if there is an exact match they will not be able to use it. I just didnt change the code to much cause i was being lazy..

in any rate you have a great solution and i really appreciate your help Moon Shadow. Im not to familiar with jquery all though i have hear it recommended in my travels. I kinda of like to code it myself it keeps my busy with puzzles and i learn at the same time, but i will check it out. Thanks again. Now i just have to figure out a way to code your solution up.

If there are any other suggestions different from Moon Shadows id love to hear. Maybe id take a little bit from all of them. Thank you again for your time u have been most helpful.

Moon Shadow
Paranoid (IV) Inmate

From: Paris, France
Insane since: Jan 2003

IP logged posted posted 09-17-2009 16:43 Edit Quote

Ahh, sorry I misunderstood you.

Try to write something like :

$response = "{property1: '1', property2: '2'}";


var myObject = eval(xmlhttp.responseText);

It should work fine as long as your responseText is short and simple.
Otherwise, you might consider using the prototype keyword to define your own objects.

See for more information about it.

If wishes were fishes, we'd all cast nets.

Obsessive-Compulsive (I) Inmate

Insane since: May 2010

IP logged posted posted 05-03-2010 13:23 Edit Quote

edit tp: spam removed.

(Edited by Tyberius Prime on 05-03-2010 14:04)

Post Reply
Your User Name:
Your Password:
Login Options: Remember Me On This Computer
Your Text:
Options: Show Signature
Enable Slimies
Enable Linkwords

« BackwardsOnwards »

Show Forum Drop Down Menu