views:

75

answers:

1

Hello i am learning comet and i am facing problem since most of the online tutorials about comet contains js prototype so can you convert this code into long polling.... thank you... -pradeep

//This is my js
var xmlhttp
function ajax(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url="ajax.php?q="+str.value;

xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
result=xmlhttp.responseText;

if(result=='true')
{
document.getElementById("erroreid").innerHTML='Already exists';
document.getElementById("erroreid").style.color = '#FE728D';
document.getElementById("empid").style.background= 'Yellow';
document.getElementById("sub").disabled=true;
}
if(result=='false')
{
document.getElementById("erroreid").innerHTML='its unique';
document.getElementById("erroreid").style.color = '#66FFCC';
document.getElementById("empid").style.background = 'White';
document.getElementById("sub").disabled=false;
}
}
}

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;
}



//this is my server end code

mysql_connect(...);
mysql_select(...);
mysql_query("SELECT * FROM empdetail WHERE empid='$_GET[q]'",$con);
if()
print"true";
else print"false";


    
A: 

Just a side note (doesn't really answer your question), but you've got a SQL injection vulnerability on this line:

mysql_query("SELECT * FROM empdetail WHERE empid='$_GET[q]'",$con);

Change it to:

mysql_query("SELECT * FROM empdetail WHERE empid='" . mysql_real_escape_string($_GET[q]) . "'",$con);

As for converting to long polling, consider using the Phico library. It's fast and easy to set up, and shouldn't require a lot of code changes.

mattbasta