Move the javascript logic of the forgot password page to an external file.

This commit is contained in:
Alex Tselegidis 2020-04-27 20:20:35 +02:00
parent 788175b9de
commit 69f4daca89
2 changed files with 55 additions and 46 deletions

View file

@ -11,7 +11,7 @@
<script src="<?= asset_url('assets/ext/jquery/jquery.min.js') ?>"></script> <script src="<?= asset_url('assets/ext/jquery/jquery.min.js') ?>"></script>
<script src="<?= asset_url('assets/ext/bootstrap/js/bootstrap.min.js') ?>"></script> <script src="<?= asset_url('assets/ext/bootstrap/js/bootstrap.min.js') ?>"></script>
<script src="<?= asset_url('assets/ext/jquery-ui/jquery-ui.min.js') ?>"></script> <script src="<?= asset_url('assets/ext/jquery-ui/jquery-ui.min.js') ?>"></script>
<script src="<?= asset_url('assets/ext/datejs/date.js') ?>"></script> <script src="<?= asset_url('assets/ext/datejs/date.min.js') ?>"></script>
<script> <script>
var EALang = <?= json_encode($this->lang->language) ?>; var EALang = <?= json_encode($this->lang->language) ?>;
@ -52,7 +52,6 @@
</style> </style>
<script> <script>
$(document).ready(function() {
var GlobalVariables = { var GlobalVariables = {
'csrfToken': <?= json_encode($this->security->get_csrf_hash()) ?>, 'csrfToken': <?= json_encode($this->security->get_csrf_hash()) ?>,
'baseUrl': <?= json_encode(config('base_url')) ?>, 'baseUrl': <?= json_encode(config('base_url')) ?>,
@ -61,43 +60,6 @@
}; };
var EALang = <?= json_encode($this->lang->language) ?>; var EALang = <?= json_encode($this->lang->language) ?>;
/**
* Event: Login Button "Click"
*
* Make an ajax call to the server and check whether the user's credentials are right.
* If yes then redirect him to his desired page, otherwise display a message.
*/
$('form').submit(function(event) {
event.preventDefault();
var url = GlobalVariables.baseUrl + '/index.php/user/ajax_forgot_password';
var data = {
'csrfToken': GlobalVariables.csrfToken,
'username': $('#username').val(),
'email': $('#email').val()
};
$('.alert').addClass('hidden');
$('#get-new-password').prop('disabled', true);
$.post(url, data)
.done(function(response) {
$('.alert').removeClass('hidden alert-danger alert-success');
$('#get-new-password').prop('disabled', false);
if (response === GlobalVariables.AJAX_SUCCESS) {
$('.alert').addClass('alert-success');
$('.alert').text(EALang['new_password_sent_with_email']);
} else {
$('.alert').addClass('alert-danger');
$('.alert').text('The operation failed! Please enter a valid username '
+ 'and email address in order to get a new password.');
}
})
.fail(GeneralFunctions.ajaxFailureHandler);
});
});
</script> </script>
</head> </head>
<body> <body>
@ -127,5 +89,6 @@
</form> </form>
</div> </div>
<script src="<?= asset_url('assets/js/general_functions.js') ?>"></script> <script src="<?= asset_url('assets/js/general_functions.js') ?>"></script>
<script src="<?= asset_url('assets/js/forgot_password.js') ?>"></script>
</body> </body>
</html> </html>

View file

@ -0,0 +1,46 @@
$(function () {
'use strict';
var $form = $('form');
/**
* Event: Login Button "Click"
*
* Make an HTTP request to the server and check whether the user's credentials are right. If yes then redirect the
* user to the destination page, otherwise display an error message.
*/
function onFormSubmit(event) {
event.preventDefault();
var url = GlobalVariables.baseUrl + '/index.php/user/ajax_forgot_password';
var data = {
'csrfToken': GlobalVariables.csrfToken,
'username': $('#username').val(),
'email': $('#email').val()
};
var $alert = $('.alert');
$alert.addClass('hidden');
$('#get-new-password').prop('disabled', true);
$.post(url, data)
.done(function (response) {
$alert.removeClass('hidden alert-danger alert-success');
$('#get-new-password').prop('disabled', false);
if (response === GlobalVariables.AJAX_SUCCESS) {
$alert.addClass('alert-success');
$alert.text(EALang['new_password_sent_with_email']);
} else {
$alert.addClass('alert-danger');
$alert.text('The operation failed! Please enter a valid username '
+ 'and email address in order to get a new password.');
}
})
.fail(GeneralFunctions.ajaxFailureHandler);
}
$form.on('submit', onFormSubmit);
});