...

View Full Version : Ajax problem authenticating



cyborg911
04-11-2011, 09:54 AM
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.




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:




<!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>

devnull69
04-11-2011, 10:46 AM
With this line of 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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum