Topic: AJAX xmlhttp.responsetext and php (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=31319" 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>\

 
Hustluz
Bipolar (III) Inmate

From:
Insane since: Jun 2003

posted posted 09-17-2009 02:14

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.

code:
<html>
<head>
<script src="clienthint.js"></script>
</head>
<body>

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

</body>
</html>



2. Next will be my javascript file

code:
var xmlhttp

function showHint(str){
	if (str.length==0){
	  document.getElementById("txtHint").innerHTML="";
	  return;
	}
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Your browser does not support XMLHTTP!");
	  return;
	}
	var url="gethint.php";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function stateChanged(){
	if (xmlhttp.readyState==4){
	  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
	  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

code:
<?php
// Fill up array with names
$a[]="Anna";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";
$a[]="Hege";
$a[]="Inga";
$a[]="Johanna";
$a[]="Kitty";
$a[]="Linda";
$a[]="Nina";
$a[]="Ophelia";
$a[]="Petunia";
$a[]="Amanda";
$a[]="Raquel";
$a[]="Cindy";
$a[]="Doris";
$a[]="Eve";
$a[]="Evita";
$a[]="Sunniva";
$a[]="Tove";
$a[]="Unni";
$a[]="Violet";
$a[]="Liza";
$a[]="Elizabeth";
$a[]="Ellen";
$a[]="Wenche";
$a[]="Vicky";

//get the q parameter from URL
$q=$_GET["q"];

//lookup all hints from array if length of q>0
if (strlen($q) > 0){
	$hint="";
  	for($i=0; $i<count($a); $i++){
		if (strtolower($q)==strtolower($a[$i])){
			$hint=$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;
?>



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

Moon Shadow
Paranoid (IV) Inmate

From: Paris, France
Insane since: Jan 2003

posted posted 09-17-2009 13:10

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 :
http://jquery.bassistance.de/autocomplete/demo/

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



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

Hustluz
Bipolar (III) Inmate

From:
Insane since: Jun 2003

posted posted 09-17-2009 15:16

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

posted posted 09-17-2009 16:43

Ahh, sorry I misunderstood you.

Try to write something like :

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

[...]

var myObject = eval(xmlhttp.responseText);
alert(myObject.property1);



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 http://mckoss.com/jscript/object.htm for more information about it.

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

people123
Obsessive-Compulsive (I) Inmate

From:
Insane since: May 2010

posted posted 05-03-2010 13:23

edit tp: spam removed.

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



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu