Ajax login validation
Hey drenge!Jeg har leget lidt med noget Ajax.
Vi taler om at validere login informationer.
Til dette har jeg følgende kode:
<script language="javascript">
$(document).ready(function()
{
$("#login_form").submit(function()
{
//remove all the class add the messagebox classes and start fading
$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
//check the username exists or not from ajax
$.post("login.php",{ brugernavnet:$('#brugernavn').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
{
if(data=='yes') //if correct login detail
{
$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
{
//add message and change the class of the box and start fading
$(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
function()
{
//redirect to secure page
document.location='community/forum.php';
});
});
}
else
{
$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
{
//add message and change the class of the box and start fading
$(this).html('Login forkert').addClass('messageboxerror').fadeTo(900,1);
});
}
});
return false; //not to post the form physically
});
//now call the ajax also focus move from
$("#password").blur(function()
{
$("#login_form").trigger('submit');
});
});
</script>
Dette er koden i JS der sådan set virker som det skal.
Problemet er min php kode(tror jeg) som ikke returner det YES selvom det er rigtigt nok(hvad brugeren indtaster).
Se min PHP kode:
if(isset($_POST[send])) {
if(isset($_POST[brugernavn]) || isset($_POST[password])) {
$brugernavn = $_POST["brugernavn"];
$brugernavnet = mysql_escape_string($brugernavn);
$password = $_POST["password"];
$password = mysql_escape_string($password);
$password = md5($password);
$userQuery = mysql_query("SELECT id,brugernavn,password FROM brugere WHERE brugernavn='$brugernavnet'");
if(mysql_num_rows($userQuery) == 1)
{
$userArray = mysql_fetch_array($userQuery);
if($userArray["password"] == $password)
{
echo "yes";
$_SESSION['brugernavn'] = $userArray["brugernavn"];
}else{
echo "no";
}
}else{
echo "no";
}
}else{ echo "no"; }
}
PÅ FORHÅND TAK :)
Anders