0

I'm using sweet alert dialog for delete confirmation.

If user click "Yes" -> Run SQL to delete.

If user click "No" -> Do nothing, keep record.

<?php
if(isset($_POST['button_delete'])) {

echo '<script type="text/javascript">'; 
echo 'setTimeout(function (){swal({
        title: "Are you sure?", 
        text:"You will not be able to recover this course!", 
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, delete it!",
        cancelButtonText: "No, cancel please!",
        closeOnConfirm: false,
        closeOnCancel: false}, 
        function(isConfirm)
        {
            if(isConfirm)
            {
                swal("Deleted!", "Your file has been deleted.", "success");';

        $deleteCourse = mysqli_real_escape_string($conn, $_POST['button_delete']);
        $user = mysqli_real_escape_string($conn, $_SESSION['session_user']);

        mysqli_query($conn, "DELETE FROM enroll
                            WHERE CourseCode LIKE '$deleteCourse' 
                            AND UserID=$user"); 

echo '
            }
            else
            {
                swal("Cancelled", "Your file is safe.", "error");
            }})}, 100);';
echo '</script>';

?>

No matter I click "Yes" or "No", the SQL will still run and delete the record. How to make it do different function when clicking Yes/No?

Thank you for helping.

Chun Fatt
  • 21
  • 1
  • 2
  • Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Epodax Jul 27 '16 at 11:31
  • Please split your code and use ajax for this. – Anuranjan Pandey Jul 27 '16 at 12:57

1 Answers1

0

That's because the PHP code gets executed no matter what. PHP doesn't check that if it is inside the JavaScript if bracket. You have to make that check inside PHP itself.

One way is to make the button make an AJAX request after confirm button is pressed. Another is to just use plain POST when it's clicked (which I think you don't want, hence AJAX would be better).

Janno
  • 542
  • 4
  • 15