Hi,

The code below always returns Fail although I send correct username and password. Can anyone spot what I do wrong?

Thanks in advance

<?php
$username = htmlspecialchars(trim($_POST['username']));
$password = htmlspecialchars(trim($_POST['password']));

if($username == 1 && $password == 1)
{
	echo json_encode(array('result' => 'success', 'username' => 'myname');
}
else
{
	echo json_encode(array('result' => 'fail'));
}
?>
<!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>
	<script type="text/javascript" src="jquery-1.7.1.min.js"></script>

	<script type"text/javascript">
	$(document).ready(function()
	{
		$("form#login").submit(function()
		{
			var text_username = $('#text_username').attr('value');
			var text_password = $('#text_password').attr('value'); 
			
			$.ajax({
				type	: "POST",
				url		: "login.php",
				data	: "username=" + text_username + "&password=" + text_password,
				success	: function(returned)
				{
					if(returned.result == 'success')
					{
						var username = returned.username;
						$('form#login').hide();
						$('div#login_success').fadeIn();
					}
					else
					{
						$('form#login').hide();
						$('div#login_fail').fadeIn();
					}
				}
			});
		
		return false;
		});
	});
	</script>
</head>

<body>

	<form id="login" method="post">
		Username : <input type="text" id="text_username" />
		Password : <input type="text" id="text_password" />
		<br /><br />
		<button type="submit">Login</button>
	</form>
	
	<div id="login_success" style="display:none; background:green;"><p>SUCCESS</p></div>
	<div id="login_fail" style="display:none; background:red;"><p>FAIL</p></div>

</body>
</html>

The function htmlspecialchars() returns a string and you are testing it against a integer, that might be the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.