Saturday 25 March 2017

javascript - PHP and AJAX login form

Hello
I know this subject has been proposed before, but no one has done the AJAX manipulation standard like i did. I'm newbie to AJAX and jQuery




The problem I have here is that I'm not sure that the js function "ajaxPostCallManip()" reaches to "login.php"!!



I've made a couple of alert()s but nothing appears...Please help



HTML CODE



    







Login or data-transition="flip">Register









placeholder="username" />


placeholder="password" />





style="display: none; font-size: 12px;
text-align: center;">status










js CODE




// AJAX Call handler using method="POST"
function ajaxPostCallManip( str, url, toDoFunc )

{
var xmlhttp; // Request variable
if( window.XMLHttpRequest ) // For modern browsers
xmlhttp = new XMLHttpRequest;
else // For old browsers
xmlhttp = new ActiveXOBject("Microsoft.XMLHttp");

xmlhttp.onreadystatechange=toDoFunc;
xmlhttp.open("POST", url, true);
xmlhttp.send(str);

}

function check_login()
{
// Construct the POST variables [username, password]
var postStr = "username=" + $('#login_username').val() + "&"
+ "password=" + $('#login_pwd').val();

// Call the general purpose AJAX Handler Function
ajaxPostCallManip(postStr, "login.php", function()

// toDoFunc to be performed when server response is ready
{
if( xmlhttp.readyState == 4 && xmlhttp.status == 200 )
{
alert(xmlhttp.responseText);
switch(xmlhttp.responseText)
{
case "1":
$('#login_err').css({'color':'green','display':'block'})
.html('Successful Login');

break;

case "2":
$('#login_err').css({'color':'red','display':'block'})
.html('incorrect username/password')
break;

case "3":
$('#login_err').css({'color':'red','display':'block'})
.html('please fill in all fields')

break;
}
}
});
}


PHP CODE




include('dbManip.php');

echo '';

$username = $_POST['username'];
$password = $_POST['password'];

// Just to check that the POST variables arrived safely
echo "";



if(!empty($username) && !empty($password))
{
// Fetch data from database
$query = "
SELECT username,password
FROM users
WHERE username = '$username' and password = '$password';
";


// Execute query
$res = mysql_query($query);

// If there is a match for the credentials entered with the database
if(mysql_num_rows($res) == 1)
{
// Fetch information and double check credentials
while($row = mysql_fetch_assoc($res))
{
$db_username = $row['username'];

$db_password = $row['password'];
}

// Compare results with user input
if( $username == $db_username && $password == $db_password)
{
// Credentials are correct - response = 1
echo '1';
}
else

{
// Credentials are incorrect - response = 2
echo '2';
}
}
else
{
// There is no match in the database
// Credentials are incorrect - response = 2
echo '2';

}
}
else
{
// If one or both fields are empty - response = 3
echo '3';
}
?>

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...