I put everything correctly except the change which was mandatory to run the code:

$username = mysql_real_escape_string($_POST["name"]);
$password = mysql_real_escape_string(md5($_POST["pass"]));

I change it , because we have below variables in ajax.post,
$.post("login.php", { name: username, pass: password },

$username = mysql_real_escape_string($_POST["username"]);
$password = mysql_real_escape_string(md5($_POST["password"]));

I am going to use similar code in model box and definately, I am going to come again with some question, because you are very helpful.