Reputation: 736
Well guys, with the following code:
$(document).ready(function() {
$(".list-book-description").css("display", "none");
$("#user-panel-login").css("display", "none");
$('#login-form').submit(function(e) {
e.preventDefault();
var formUsername=$("#login-form #username").val();
var formPassword=$("#login-form #password").val();
if((formUsername.length<6))
{
$("#ajax-output").html("<div class='error'>Attenzione username troppo breve!</div>");
}
else if((formPassword.length<6))
{
$("#ajax-output").html("<div class='error'>Attenzione password troppo breve!</div>");
}
else
{
$.post(
'index.php?module=login',
{
"username": formUsername,
"password": formPassword,
},
function(data){
$("#ajax-output").html(data);
}
);
}
});
});
i tried to pass to ?index.php&module=login&function=1
username and password got by #login-form
fields #username
& #password
to login()
php function that uses setLogin();
function if data is into database to set a login session.
function Login()
{
if(!checkLogin())
{
$function=@stringEscape($_GET['function']);
if($function==1)
{
$username=stringEscape($_POST['username']);
$password=sha1(stringCrypt($_POST['password'], 'sha1').stringCrypt(DB_CRYPT, 'sha1'));
if(setLogin($username, $password))
{
echo "<div class='ok'>Login effettuato con successo, tra pochi secondi verrai riportato alla homepage!</div>";
}
}
}
else notfound();
}
function setLogin($username, $password, $time="")
{
if(!checkLogin())
{
if((isset($username)) && (isset($password)))
{
$query=mysql_query("SELECT * FROM ".DB_PREF."users WHERE user_username='".$username."' AND user_password='".$password."'");
if(mysql_num_rows($query))
{
$fetch=mysql_fetch_assoc($query);
$_SESSION['logged_user_id']=$fetch['user_id'];
$_SESSION['logged_user_username']=$fetch['user_username'];
return true;
}
else
{
echo "<div class='error'>Dati non validi, ripeti la procedura di login o <a href='?index.php&module=registration'>registrati</a>!</div>";
}
}
}
else notfound();
}
Now i've a problem when i get the result of $.post()
in my #ajax-input
div submitting the login form, it doesn't return just the result of login function, but the index page+login function result.
If i try to set "json" parameter into $.post
function and echo json_encode()
in php function it doesn't work at all. So, what could be the problem?
Upvotes: 0
Views: 114
Reputation: 7517
you make a request for a full page here, I think.you could make a parameter, post or get, you decide, and if that is for example true, then you return the full page, and he it's false you only return a JSON string or something like that. I believe it is even possible to determine if the request is from ajax or not! That could be your second option. Just add a simple if-statement, I think.
Upvotes: 2