I use Sweet Alert 2 for nice dialogs.
Now I want to use it for a security question if an database entry should be delete or not. It’s no problem to show the dialog, but I can’t find a way to integrate a function if the delete is confimred. Could anyone please help me?
Thats the code for now:
JavaScript
x
27
27
1
<script>
2
Swal.fire({
3
title: '',
4
text: "Diesen Eintrag wirklich löschen?",
5
icon: 'warning',
6
showCancelButton: true,
7
confirmButtonText: 'Ja',
8
cancelButtonText: 'Nein',
9
reverseButtons: true
10
}).then((result) => {
11
if (result.isConfirmed) {
12
Swal.fire(
13
'<Here a function if delete is confirmed>'
14
)
15
} else {
16
Swal.fire(
17
'<Here a function to go back>'
18
)
19
}
20
})
21
</script>
22
23
<?php
24
public function deleteConfimred(){
25
// Delete the entry
26
}
27
Advertisement
Answer
You could submit a form using JS which submits to the PHP file where your function is.
HTML:
JavaScript
1
23
23
1
<form id="yourFormId" action="yourphpfile.php" method="post">
2
<input type="hidden" name="delete" value="">
3
</form>
4
<button type="button" onclick="confirmDelete()">Delete</button>
5
6
<script>
7
function confirmDelete() {
8
Swal.fire({
9
title: '',
10
text: "Diesen Eintrag wirklich löschen?",
11
icon: 'warning',
12
showCancelButton: true,
13
confirmButtonText: 'Ja',
14
cancelButtonText: 'Nein',
15
reverseButtons: true
16
}).then((result) => {
17
if (result.isConfirmed) {
18
document.getElementById("yourFormId").submit();
19
}
20
})
21
}
22
</script>
23
yourphpfile.php:
JavaScript
1
10
10
1
<?php
2
if(isset($_POST['delete'])) {
3
deleteConfirmed();
4
}
5
6
public function deleteConfirmed(){
7
// Delete the entry
8
}
9
?>
10