Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Ajax problem authenticating

    I am trying to use Ajax to accept userid and password and authenticate it with PHP mySQL(id varchar pass varchar). Right now i am just checking for the username not the password. Please help.

    Code:
     function showUser(str)
    {
    if (str=="")
      {
      document.getElementById("txt1").innerHTML="";
      return;
      } 
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("txt1").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("POST","idpass.php?q="+str,true);
    xmlhttp.send();
    }
    
    <form onSubmit="showUser(username.value)">
      <div id="txt"><b>Enter your username </b>
        <input type="text" name= "username">
        <br>
      <b>Enter your password</b> <input type="password" name="pass">
      <br></div>
      <div id="txt1" style="display : none">help</div>
      <input type="submit" value="Login" />
    </form>
    The javascript code is as follows:

    Code:
    <!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>Id Pass Authentication</title>
    </head>
    <body>
    <?php
    
    $con = mysql_connect("localhost", "IDNAME", "PASSWORD");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("my_db", $con);
    
    $sql="SELECT * FROM persons WHERE id = '".$_POST[username]."'";
    
    $result = mysql_query($sql);
    $id = $_POST[username];
    $pass=$_POST[pass];
    
    $row = mysql_fetch_array($result);
    
    if($row['id']==$id)
    	{
    		echo "You have logged in!";
    	}
    	else
    	{
    		echo "sorry you are not logged in!";
    	}
    
      
     
      
    mysql_close($con);
    ?>
    </body>
    </html>
    Last edited by cyborg911; 04-11-2011 at 08:56 AM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    With this line of code
    Code:
    xmlhttp.open("POST","idpass.php?q="+str,true);
    you are trying to start a POST type request with GET type parameters. This won't work. Just change the POST to GET. The PHP script will have access to the parameters in $_GET[] then.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •