please, can you help me with this? I am a beginner in programming, so if it’s a stupid question, have a patience with me. Thank you.
I have index.php page with login form (username, password) and login.php with this part of a code:
<? if(!$_POST['username'] || !$_POST['password']){
$data['msg'] = 'All fields have to be filled!';
$data['success'] = false;}else{
mysql_query(" INSERT INTO log_access(user,ip,datetime)
VALUES(
'".$_POST['username']."',
'".$_SERVER['REMOTE_ADDR']."',
NOW()
)");
$row = mysql_fetch_assoc(mysql_query("SELECT id,login FROM users WHERE login='".$username."' AND pwd='".md5($password)."'"));
if($row['login'])
{
$_SESSION['login'] = $row['login'];
$_SESSION['id'] = $row['id'];
$data['success'] = true;
$data['redirect'] = "index.php";
}
else
{
$data['success'] = false;
$data['msg']='Wrong username or password!';
}}echo json_encode($data);?>
If i send empty field(s) in username or/and password, login.php return $data. But if i send filled fields, no data is comming back to index.php.
I have the following in index.php:
<script>
$(document).ready(function()
{
$('#frmLogin').submit(function()
{
$.post('/scripts/login.php',{username: $('[name=username]').val(), password: $('[name=password]').val()}, function(data)
{
if(data.success)
{
$('#login-msg').html("Logged");
}
else
{
$('#login-msg').html(data.msg);
}
}
,'json');
return false;
});
});
</script>
I have the same problem if I put anything in the first condition, or before it (in login.php).
I don’t know, what i’m doing wrong 🙁
Thank for any help!
Washa
EDIT
SOLVED!
I have bad connection to the DB from the login script. Beginners mistake. Thanks for all replies.
Can you check Firebug’s console and see if the POST is happening and the response code is 200? you can also check what response is being retrieved.
try calling a test.php with the following contents:
Also check if the username and password are being passed in correctly by doing
just above $.post.
I checked your js by hardcoding username and password, they are working fine.
its usually a good idea to divide and conquer. hardcode the input for your login.php page, and run it first, after its working then make your $_POST params as the input. this way you could prevent a lot of problems.