Created
November 9, 2017 10:02
-
-
Save EvilWolf/b9ee2268b1ca0932b3d8729a7b5caf96 to your computer and use it in GitHub Desktop.
Вариант реализации регистрации на AJAX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function(){ | |
var $registerForm = $('#applicationRegisterForm'); | |
/* Если форм на странице нет, то скрипт тут завершит свою работу */ | |
if ($registerForm.length === 0) | |
return; | |
/* Валидатор, наверно можно удалить. */ | |
$registerForm.find('.form-control').on('change', function(){ | |
var $this = $(this); | |
$this.removeClass('parsley-error'); | |
}); | |
/* Основное событие отправки формы. */ | |
$registerForm.on('submit', function(event) { | |
event.preventDefault(); | |
var $Form = $(this); | |
var data = $Form.serialize(); | |
data += '&method=AjaxRegister'; | |
$Form.find('.form-control').removeClass('parsley-error'); | |
$Form.find('[type="submit"]').addClass('disabled btn-ajax-loading').attr('disabled', 'disabled'); | |
$.ajax({ | |
url: '/ajax.php', | |
method: 'POST', | |
data: data, | |
success: function(response, status, jXHR) { | |
if (response['STATUS']) { | |
/* Пришла ошибка */ | |
var $FormMessage = $('#formMessage'); | |
$FormMessage.slideUp(100, function(){ | |
$FormMessage.html('<p class="text-danger">'+response['MESSAGE']+'</p>'); | |
$Form.find('.form-control').addClass('parsley-error'); | |
$FormMessage.slideDown(200); | |
}); | |
} else if (response['STATUS'] === 0 && response['REDIRECT']) { | |
/* Пришло всё ОК, перекидывает внутрь */ | |
window.location.href = response['REDIRECT']; | |
} else { | |
/* Пограничный случай, если пришло что-то не то */ | |
var $FormMessage = $('#formMessage'); | |
$FormMessage.slideUp(100, function(){ | |
$FormMessage.html('<p class="text-danger">Ошибка регистрации</p>'); | |
$FormMessage.slideDown(200); | |
}); | |
} | |
$Form.find('[type="submit"]').removeClass('disabled btn-ajax-loading').attr('disabled', false); | |
}, | |
error: function(jXHR) { | |
/* Пограничный случай, если пришло что-то не то */ | |
var $FormMessage = $('#formMessage'); | |
$FormMessage.slideUp(100, function(){ | |
$FormMessage.html('<p class="text-danger">Ошибка регистрации</p>'); | |
$FormMessage.slideDown(200); | |
}); | |
$Form.find('[type="submit"]').removeClass('disabled btn-ajax-loading').attr('disabled', false); | |
} | |
}) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment