Ajax login with div refresh?
I am new to ajax and javascript, i want to replace my php/mysql login system using ajax so that my div's refresh if user is logged in successfully without pag开发者_StackOverflow社区e redirection.
Let's say you have content <div id="login"></div>
and the following jQuery:
$(document).ready(function () {
$.ajax({
url: 'login.php',
success: function (data) {
$('#login').html(data);
}
});
Then in your login.php file:
<?php
session_start();
if ($_SESSION['isLoggedIn']) {
header('Location: my_account.php'); //This is the page to show if user is logged in
exit;
}
?>
<form action="login_process.php" method="post">
...
</form>
This way, when the client's request fires the ajax to load the div#login content, if the user is logged in already, the content loaded will be from the my_account.php
page instead.
Well, this is incomplete...but thats the concept
$.post("login.php", { name: "John", pass: "1234" },
function(data) {
alert("Data Loaded: " + data);
if (data == 'OK') {
$('#divlogin').hide();
$('#otherdiv').show();
}
});
精彩评论