I just started using ajax and I am having mixed results. Here is the code: On the page that calls the script: echo "<input type='text' id='txt1' value='$name' onmousemove=\"showHint(this.value)\" />"; PHP: The script that gets called: $s = $_GET["s"]; $s = mysql_real_escape_string($s); $ck = mysql_query("SELECT * FROM `user` WHERE `username` = '$s'"); if (mysql_num_rows($ck) > 0) { mysql_query("DELETE FROM `user` WHERE `username` = '$s'"); echo "<h1>User Deleted!</h1>"; } else { echo "<h1>Not Deleted Yet!</h1>"; } PHP: What happens, however, is that nothing gets echoed, but the delete query works perfectly. No "user deleted" or "not deleted yet" -- if I remove the delete query, nothing happens.
All the variables are defined and everything works including the sql query. What doesnt work is the echoing of text. What more of the code would you like to see?
The ShowHint() function for starters, that way we can actually see if its a problem with the ajax or a problem with the script.
It is copied almsot exactly from an example on w3schools. Hope this helps. var xmlhttp function showHint(str) { if (str.length==0) { document.getElementById("sid").innerHTML=""; return; } xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Your browser does not support XMLHTTP!"); return; } var url="check.php"; url=url+"?s="+str; xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("sid").innerHTML=xmlhttp.responseText; } } 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; } PHP:
Look into JQuery, Mootools or Prototype, since your function could simply be this (with jquery), and it would handle cross-browser compatbility far better than raw xmlhttp. function showHint(str) { if (str.length==0) { $('#sid').html(''); return; } $('#sid').load('check.php?s='+str); //$('#sid').load('check.php', {s:str}); would be sent as a post request } Code (markup):