itskp-odense/Frontend/Assets/Ajax/editPost.js

56 lines
2.6 KiB
JavaScript

function editPost(id) {
$.ajax({
type: 'POST',
url: '../Backend/controller/getPost.php',
data: { id: id },
success:function (data) {
Swal.fire({
title: 'Rediger Post',
html: '<div class="form-group row mb-4 justify-content-center"><label for="title" class="col-md-12 col-form-label text-md-right">Titel</label><div class="col-md-9"><input type="text" class="form-control" id="title" name="title" maxlength="255" value="'+ data.title +'" required autofocus></div></div><div class="form-group row mb-4 justify-content-center"><label for="title" class="col-md-12 col-form-label text-md-right">Beskrivelse</label><div class="col-md-9"><textarea type="text" class="form-control" id="description" name="description" maxlength="2000" required autofocus>'+ data.description +'</textarea>',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Rediger',
focusConfirm: false,
preConfirm: () => {
const title = Swal.getPopup().querySelector('#title').value
const desc = Swal.getPopup().querySelector('#description').value
if (!title || !desc) {
Swal.showValidationMessage(`Skriv noget i begge felter!`)
} else {
data.title = title;
data.description = desc;
}
return {title: title, desc: desc}
}
}).then((result) => {
if (result.isConfirmed)
$.ajax({
type: 'POST',
url: '../Backend/controller/update.php',
data: {'id':id, 'upTitle': data.title, 'upDescription': data.description},
success:function () {
Swal.fire(
'Posten er redigeret!',
'',
'success'
).then(function() {
document.getElementById(id).children[1].innerHTML = data.title;
})
},
error:function (data) {
Swal.fire(
'Posten kunne ikke blive redigeret',
'',
'error'
)
}
});
})
},
error:function (data) {
console.log(data);
}
});
/**/
}