-1

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.

Darth Mikey D
  • 95
  • 1
  • 12
  • It calls both functions because PHP is server-side, while javascript is client-side. It will do both as you have it, to fill the html before handing it to the user. – IncredibleHat Jul 19 '18 at 14:13
  • And I have to say, the frightening part of this code is your `unlink` loop on all files in a directory based off unsanitized user input... and the `rmdir`... and the sql query... – IncredibleHat Jul 19 '18 at 14:22
  • Please see edit. – Darth Mikey D Jul 19 '18 at 17:28
  • "*When the page loads, it will load the PHP functions, but it **shouldn't run them until they are called**.*" In your code, you **are** calling those functions. "*it should run the deletepost javascript function, and at that point it **should call the deletepostphp php function***" ... no. It will not, not unless you make a new request to the server (direct or via ajax). PHP will call its functions as you have them during that initial request. PHP is generating html for output along the way. Once it gets to the users computer, no php functions can be 'called' in the html. – IncredibleHat Jul 19 '18 at 17:36

1 Answers1

-2

You are putting a php function into javascript variable, first of all a javascript variable doesn't accept php variable without echoing. And also your coding style is not good, please create external file for both php and javascript.

  • "first of all a javascript variable doesn't accept php variable without echoing" — I see an `echo` in the code – Quentin Jul 19 '18 at 14:30
  • "please create external file for both php and javascript" — The PHP depends on data in the URL of the current page to output the correct JS. Refactoring it to avoid that is a lot more significant than just moving the JS to a different file. – Quentin Jul 19 '18 at 14:31