My question is this. I have a website that I am developing and I am facing the following problem that I can not solve. I make queries against mysql with php and I keep it in cookies errors or well-made queries, and I want the user on the screen to get notified that it has been completed correctly. For the notifications I am using the following js / Jquery, it is designed to run when I receive a click of a button id, but I want to execute it when this query is finished. Can you help me?
$(document).ready(function () {
"use strict";
var i = -1;
var toastCount = 0;
var $toastlast;
var getMessage = function () {
var msgs = ['Ejemplo de notificación'];
i++;
if (i === msgs.length) {
i = 0;
}
return msgs[i];
};
$('#newLista_alert').click(function () {
var shortCutFunction = 'success';
var msg = 'Preparada para meterle canciones';
var title = 'Lista creada existosamente' || '';
var $showDuration = $(300);
var $hideDuration = $(1000);
var $timeOut = $(2000);
var $extendedTimeOut = $(500);
var toastIndex = toastCount++;
var addClear = $('#addClear').prop('checked');
toastr.options = {
closeButton: true,
debug: false,
newestOnTop: false,
progressBar: true,
positionClass: 'toast-bottom-right' || 'toast-top-right',
preventDuplicates: false,
onclick: null
};
toastr.options.showEasing = 'swing';
toastr.options.hideEasing = 'linear';
toastr.options.showMethod = 'fadeIn';
toastr.options.hideMethod = 'fadeOut';
var $toast = toastr[shortCutFunction](msg, title); // Wire up an event handler to a button in the toast, if it exists
$toastlast = $toast;
if (typeof $toast === 'undefined') {
return;
}
if ($toast.find('#okBtn').length) {
$toast.delegate('#okBtn', 'click', function () {
alert('you clicked me. i was toast #' + toastIndex + '. goodbye!');
$toast.remove();
});
}
if ($toast.find('#surpriseBtn').length) {
$toast.delegate('#surpriseBtn', 'click', function () {
alert('Surprise! you clicked me. i was toast #' + toastIndex + '. You could perform an action here.');
});
}
if ($toast.find('.clear').length) {
$toast.delegate('.clear', 'click', function () {
toastr.clear($toast, { force: true });
});
}
});
function getLastToast() {
return $toastlast;
}
$('#clearlasttoast').click(function () {
toastr.clear(getLastToast());
});
$('#cleartoasts').click(function () {
toastr.clear();
});
});
MY CODE FOR MYSQL
<?php
session_start();
if(isset($_REQUEST['newLista'])){
$nombrelista = $_REQUEST['nombrelista'];
$usuario = $_SESSION['usuario'];
require_once "../biblioteca.php";
$db = ConectaDb($dbHost, $dbUser, $dbPass, $dbName);
$consulta="INSERT INTO lista_musica (nombre,usuario) VALUES (:lista,:usuario)";
$result=$db->prepare($consulta);
if ($result->execute([":lista" => $nombrelista, ":usuario" => $usuario,])){
setcookie('result_musica','0',time()+10);
header ('location: ../../ui-musica.php');
}
else{
echo "LISTA NO SE PUDO CREAR";
}
}
?>