Jump to content

Problem In Ajax


msafvati

Recommended Posts

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);
?>

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.