What is happening is I have Sweetalert2 on my page. It displays the alert fine, but when I click cancel, it runs both php functions, and not just the one needed.
What should happen is when I click cancel, it takes me back to the page for the post being deleted, otherwise it deletes the post and takes me back to the list.
Here is my code. Any help or suggestions are appreciated.
<!doctype html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.25.0/sweetalert2.all.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.25.0/sweetalert2.min.css" />
<?php
function dontdeletepostphp(){
$id = $_GET["id"];
$album = $_GET["album"];
$url = "viewpost.php?id=" . $id;
echo $url;
}
function deletepostphp(){
include('db_connect.php');
$post = $_GET["id"];
$albumnum = $_GET["album"];
if($albumnum <> 0){
$files = glob('photoalbums/' . $albumnum . '/photos/*');
foreach($files as $file){
if(is_file($file)){
unlink($file);
}
}
$files2 = glob('photoalbums/' . $albumnum . '/thumbs/*');
foreach($files2 as $file){
if(is_file($file)){
unlink($file);
}
}
rmdir("photoalbums/" . $albumnum . "/photos");
rmdir("photoalbums/" . $albumnum . "/thumbs");
rmdir("photoalbums/" . $albumnum);
$sqlalbum = "delete from photoalbum where id=" . $albumnum;
mysqli_query($dbcon, $sqlalbum);
}
$sqlpost = "delete from posts where id=" . $post;
mysqli_query($dbcon, $sqlpost);
mysqli_close($dbcon);
}
?>
<script>
function dontdeletepost(){
var x="<?php dontdeletepostphp(); ?>";
window.location = x;
}
function deletepost(){
var x="<?php deletepostphp(); ?>";
swal({
title: 'Post Deleted',
text: 'Post has been deleted. Returning to the posts page.',
type: 'success'
}).then(function() {
window.location = 'states.php';
});
}
function confirmdelete(){
swal({
title: 'Delete Post',
text: 'Are you sure you want to delete this post? You cannot undo this.',
type: 'warning',
showCancelButton: true,
confirmButtonText: 'Delete Post',
cancelButtonText: 'Cancel'
}).then((result) => {
if (result.value) {
deletepost();
}else{
dontdeletepost();
}
})
}
</script>
<title>Mountain Biking In The US!!</title>
</head>
<body>
<?php
echo "<script>confirmdelete();</script>";
?>
</body>
</html>
**** EDIT ****
Thanks for all the responses so far.
I modified the code in the body, taking out the php and leaving the javascript code as follows (though I don't think this will change the way the code runs, just realized I didn't need the php tags):
<body>
<script>confirmdelete();</script>;
</body>
I understand that the PHP will be loaded before Javascript, but lets see if my thinking is correct.
When the page loads, it will load the PHP functions, but it shouldn't run them until they are called.
Then, the javascript will load, at which point the confirmdelete javascript function will be run, creating the Sweetalert2 confirmation box. If I click Delete Post, it should run the deletepost javascript function, and at that point it should call the deletepostphp php function.
And if I click Cancel, it should then run the dontdeletepostphp php function.
I don't think the php functions are running until after the Sweetalert2 prompt pops up and I make a choice, just not sure why both functions are being run. I thought that the php functions wouldn't actually run till called. I loaded the page with just the php code, and the functions didn't run without being called, as they should have.
Thanks again for the help.