Jump to content

Ajax Combined With Xmlhttprequest()?


sunicani

Recommended Posts

AJAX combined with xmlHttpRequest()?well, I've seen some of topics about whether a better solution for fast response on server via Javascript or PHP combined with HttpRequest() function in order to load data to dynamic page!in my opinion, I want to use AJAX with xmlHttpRequest.to be frank, I am a learner for HttpRequest(), and, want an example for this function. to be specified, how to display data via this way?thanks a lot, any experts!

Link to comment
Share on other sites

again, I can't figure out where to start with httprequest for data in MySQL, use xmlHttpRequest()?, or AJAX coe mbined with HttpRequest()?, or just pure PHP combined with MySQL?by the way, it seems xml is more faster? however, so many nodes with be complicated?in my opinion, I prefer to pure Javascript (AJAX) alike, or just PHP+MySQLany experts who could guide me in detail.thanks you all, experts, yeah, my mates all here!

Link to comment
Share on other sites

There is no "HttpRequest" object in Javascript. It's called "XmlHttpRequest". But ignore the XML part because it has little to do with XML.The "XmlHttpRequest" object is what you use to make HTTP requests.

Link to comment
Share on other sites

I am a learner for HttpRequest(),
Do you mean the PHP function? If so I think that makes a call in the PHP script ie before the page gets to your browser so is not really relevant to Ajax.
Link to comment
Share on other sites

...in my opinion, I prefer to pure Javascript (AJAX) alike, or just PHP+MySQL
What works for me using javascript and PHP is something like this (this is example of filtering a name search from my site using AJAX;
var xmlHttpfunction findName(str){    var x = document.getElementById("txtHint");	if (str.length == 0)   {		document.getElementById("txtHint").style.display="none";		return;	}	document.getElementById("txtHint").style.display="";	xmlHttp=GetXmlHttpObject();	if (xmlHttp==null)	{	   	alert ("Your browser does not support AJAX!");		  return;	}	var url="yourPHPScipt.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;	}}function GetXmlHttpObject(){ 	var xmlHttp=null;	try	  {	   	// Firefox, Opera 8.0+, Safari		  xmlHttp=new XMLHttpRequest();	  }	catch (e)	  {	   	// Internet Explorer		  try		{		 	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");		}		  catch (e)		{		 	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");		}	  }	return xmlHttp;}

And the PHP script;

<?include("/includes/db.php");$queryString = $_GET["q"];function is_upper($input) {   return ($input == strtoupper($input));}if (strlen($queryString) > 0){ 	echo "<menu name=cid>";	if (is_numeric($queryString))		$query = mysql_query("SELECT * FROM youtable WHERE id like '$queryString%' order by id limit 10");	else if (is_upper($queryString))		$query = mysql_query("SELECT * FROM yourtable WHERE initials like '$queryString%' order by initials limit 10");	else		$query = mysql_query("SELECT * FROM yourtable WHERE name like '%$queryString%' order by name limit 20");	$i=0;	if($query)	{		while ($result = mysql_fetch_object($query))		{			$aid = sprintf("id%d", $i++);			$string = sprintf("%-7.7s  %s (%s)", $result->cid, $result->name, $result->initials);			  echo "<li class=\"suggestionListli\" onmouseOver=\"mouseOver($i)\" onmouseOut=\"mouseOut($i)\" id=$i onclick=\"getCid('$result->cid', '$string')\">$string</li>";		  }	}	echo "</menu>";}?>

And finally the HTML snippet:

<html>...<input class="bold" type="text" size="45" id="inputString" name="searchbox" onkeyup="findName(this.value);"  /></p><div style="display: none;" class="suggestionsBox" id="txtHint"></div><br>...</html>

NOTE: forgive the sprintf functions, I'm a seasoned C programmer, old habits die hard!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...