I am new in AJAX. I'm developing a site where a user can post details about there item. User can insert data into the form without logging in but when the user clicks submit
, an AJAX script will check if the user session exist or not, if not login modal will appear.
What I'm trying to say here is as soon as the user logs in the item form will be submitted instantly and automatically with AJAX.
I did it in this way, when user clicks item form submit a script check for user session, if session does not exists, the login modal will appear via AJAX and I set a JavaScript timer after a certain interval which will check if still the user has logged in or not.
This is my jQuery script
$(function() {
$('form').on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'ajxprocess/checksignin.php',
success: function(result) {
if (result == '0') {
$.ajax({
cache: false,
url: 'ajxload/signin_gl.php',
success: function(result) {
$('#static').modal('show');
$('#static').html(result);
var myTimer = setInterval(function() {
checksignin()
}, 500);
}
});
} else {
$.ajax({
type: 'post',
url: 'ajxprocess/finalprocess.php',
data: $('form').serialize(),
beforeSend: function() {
$('#final').html("<img src='assets/img/input-spinner.gif' />");
},
success: function(html) {
if (html == '1') {
window.location = 'posts.php';
} else {
$('#final').html('Somthing went wrong during the process!');
}
}
});
}
}
});
});
});
And this is my login checker
function checksignin() {
$.ajax({
url: 'ajxprocess/checksignin.php',
success: function(result) {
if (result !== '0') {
$.ajax({
type: 'post',
url: 'ajxprocess/finalprocess.php',
data: $('form').serialize(),
beforeSend: function() {
$('#final').html("<img src='assets/img/input-spinner.gif' />");
},
success: function(html) {
if (html == '1') {
window.location = 'posts.php';
} else {
$('#final').html('Error occured during the process!');
}
}
});
}
}
});
}
The problem is I don't want to use timer. Is there any other process to do it?