msafvati Posted October 18, 2009 Share Posted October 18, 2009 hi my friends i want to use Ajax in php,but when i use from it i encounter with a problem. that is:"i send a request to server but it dont give me Response" please see this code and guide me, thank you <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript"> 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; } function checkUser() { xmlHttp=GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; } url='http://127.0.0.1/mas/index.php?user='+document.getElementById('user').value; //alert(url); xmlHttp.onreadystatechange=stateChanged; xmlHttp.open("GET",url,true); xmlHttp.send(null); } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { if (xmlHttp.responseText==1) { document.getElementById("user").style.border="1px solid #FF0000"; } else{ document.getElementById("user").style.border="1px solid #00CC00"; } } } </script> </head> <body> <form action="http://127.0.0.1/mas/index.php" method="get"> <table width="300" border="0"> <tr> <td><label for="name">name </label></td> <td><input type="text" id="name" /></td> <td> </td> </tr> <tr> <td><label for="user">user </label></td> <td><input type="text" id="user" /></td> <td style="text-align:left"><input type="button" value="check" onclick="checkUser()"/></td> </tr> <tr> <td><label for="pass">password</label></td> <td><input type="password" id="pass" /></td> <td> </td> </tr> <tr> <td></td> <td colspan="2"><input type="submit" id="send" /></td> </tr> </table> </form> </body> </html> and PHP: <?php $con=mysql_connect('127.0.0.1','root','root'); if($con){ echo "Connection Successfuly \n"; } $db=mysql_select_db('Ajax',$con); $sql="SELECT * FROM `users` WHERE `user` ='".$_GET['user']."'"; $r=mysql_query($sql); $result=mysql_num_rows($r); echo( $result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/178117-problem-in-ajax/ Share on other sites More sharing options...
ialsoagree Posted October 18, 2009 Share Posted October 18, 2009 I don't know if this is your problem but your PHP isn't written well <?php $con=mysql_connect('127.0.0.1','root','root'); // DO THE FOLLOWING ONLY ON SUCCESSFUL DATABASE CONNECTION if($con){ echo "Connection Successfuly \n"; } // DO THE FOLLOWING EVEN IF THERE IS NO DATABASE CONNECTION $db=mysql_select_db('Ajax',$con); $sql="SELECT * FROM `users` WHERE `user` ='".$_GET['user']."'"; $r=mysql_query($sql); $result=mysql_num_rows($r); echo( $result); ?> See the problem? You check if $con successfully connected, but even if it didn't, you still try to run a query. Also, I hope you realize that your $db variable is a boolean. mysql_select_db returns a boolean that represents whether or not it worked (TRUE if it worked, FALSE otherwise). Also, you're concatenating your variable to a string that's being parsed for variables: $sql="SELECT * FROM `users` WHERE `user` ='".$_GET['user']."'"; Because you use double quotes, PHP is searching the string for variables (parsing the string). But you remove your variable from the string (".$_GET['user'].") so there's nothing for PHP to find! You should do this instead: $sql="SELECT * FROM `users` WHERE `user` ='$_GET[user]'"; You also aren't escaping the user input! This makes it easy for a malicious user to inject SQL into your form that your database will then process! Quote Link to comment https://forums.phpfreaks.com/topic/178117-problem-in-ajax/#findComment-939389 Share on other sites More sharing options...
msafvati Posted October 19, 2009 Author Share Posted October 19, 2009 thank u, i do your tells but dont give any answer from Server,responseText from Server is true(Example if my Data Exist in Db Return 1)but if block have problem, plz guid me!!! Quote Link to comment https://forums.phpfreaks.com/topic/178117-problem-in-ajax/#findComment-939558 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.