rDrama/files/assets/js/post_toast2.js
2021-12-31 15:08:40 +02:00

36 lines
No EOL
1.1 KiB
JavaScript

function post_toast2(url, button1, button2) {
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
var form = new FormData()
form.append("formkey", formkey());
if(typeof data === 'object' && data !== null) {
for(let k of Object.keys(data)) {
form.append(k, data[k]);
}
}
form.append("formkey", formkey());
xhr.withCredentials=true;
xhr.onload = function() {
try {
let data = JSON.parse(xhr.response)
if (xhr.status >= 200 && xhr.status < 300 && !data['error']) {
document.getElementById('toast-post-success-text').innerText = data["message"];
new bootstrap.Toast(document.getElementById('toast-post-success')).show();
document.getElementById(button1).classList.toggle("d-none");
document.getElementById(button2).classList.toggle("d-none");
} else {
if (data["error"]) document.getElementById('toast-post-error-text').innerText = data["error"];
new bootstrap.Toast(document.getElementById('toast-post-error')).show();
}
}
catch(e) {new bootstrap.Toast(document.getElementById('toast-post-error')).show();}
};
xhr.send(form);
}