I am stuck with ajax response, that always go in else part and it print no in session’s class called statusmsg and also add meta tag.
It is fine in localhost but not in live site.
Output:
<div class="statusmsg">
Welcome
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled Document</title>
no
</div>
Here is my login code with ajax jquery
$.ajax({
type: "POST",
url:"scripts/ajax_login.php",
data:"account="+account+"&pswd="+password,
beforeSend: function()
{
$(".login_result").show();
},
success: function(resp)
{
if($.trim(resp) == 'no')
{
$(".warn_msg").html("Wrong Username/Email And/or Passwrd");
return false;
}
else
{
$(".statusmsg").html("Welcome "+$.trim(resp));
$(".loginArea").hide();
$(".sh_logout").show();
}
},
complete: function()
{
$(".login_result").hide();
},
error: function(e)
{
alert('Error: ' + e);
}
}); //end Ajax
}//else
return true;
}//function
=========================================
ajax_login.php
session_start();
include(dirname(dirname(__FILE__))."/includes/config.php");
$account = mysql_real_escape_string($_POST['account']);
$password = mysql_real_escape_string(md5($_POST['pswd']));
$sql = (strpos($account,'@')===false)?"SELECT * FROM register_members WHERE username = '".$account."' AND password = '".$password."'":"SELECT * FROM register_members WHERE password = '".$password."' AND email = '".$account."' ";
$qry = mysql_query($sql);
$msg = mysql_num_rows($qry); //returns 0 if not already exist
$row = mysql_fetch_assoc($qry);
if(!$msg > 0)
{
echo trim('no');
}
else
{
$_SESSION['user'] = $row['username'];
$user = trim($_SESSION['user']);
echo trim($user);
}
Should be
Else you are negating the value in $msg. If this is 0 it becomes 1 and if it’s 1 it becomes 0. (Basic negation of a boolean value).
So neither 1 nor 0 will ever be bigger than 0 and thus it will never
echo trim('no');.But basically you can just check if
$msgequals to zero like: