Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-11-2011, 08:54 AM   PM User | #1
cyborg911
New Coder

 
Join Date: Apr 2011
Posts: 23
Thanks: 1
Thanked 0 Times in 0 Posts
cyborg911 is an unknown quantity at this point
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..
cyborg911 is offline   Reply With Quote
Old 04-11-2011, 09:46 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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.
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:48 AM.


Advertisement
Log in to turn off these ads.