Bat ganito

loktoy

Established
nadedelete yun sya kaso yun problima is
di lumalabas yun message na ito:

Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
).then(function() {
location.reload();
});

kaso ito lumalabas kapag yes delete ko na :

error: function() {
Swal.fire(
'Error!',
'An error occurred while deleting the file.',
'error'
pero nadedelete sya sa database

ito code ko:

$('.btn-delete').on('click', function(e) {
e.preventDefault();
const href = $(this).attr('href')
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
url: 'commentdelete.php',
type: 'POST',
data: {comment_id: <?php echo $id_comment; ?>},
success: function(data) {
if (data == 'success') {
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
).then(function() {
location.reload();
});
} else {
Swal.fire(
'Error!',
'An error occurred while deleting the file.',
'error'
);
}
},
error: function() {
Swal.fire(
'Error!',
'An error occurred while deleting the file.',
'error'
);
}
});
}
});
});
 
Based on the code you provided, it seems that the issue lies in the AJAX success callback. When the data returned from the server is 'success', the success message is displayed and the page is reloaded. However, if the data is not 'success', the error message is displayed.

To further diagnose the issue, you can check the response data by adding a console.log(data); line within the success callback. This will help you determine what is being returned from the server.

Here's an updated version of your code with the console.log statement added:

JavaScript:
$('.btn-delete').on('click', function(e) {
    e.preventDefault();
    const href = $(this).attr('href')
    Swal.fire({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'
    }).then((result) => {
        if (result.isConfirmed) {
            $.ajax({
                url: 'commentdelete.php',
                type: 'POST',
                data: {comment_id: <?php echo $id_comment; ?>},
                success: function(data) {
                    console.log(data); // Check the response data
                    if (data == 'success') {
                        Swal.fire(
                            'Deleted!',
                            'Your file has been deleted.',
                            'success'
                        ).then(function() {
                            location.reload();
                        });
                    } else {
                        Swal.fire(
                            'Error!',
                            'An error occurred while deleting the file.',
                            'error'
                        );
                    }
                },
                error: function() {
                    Swal.fire(
                        'Error!',
                        'An error occurred while deleting the file.',
                        'error'
                    );
                }
            });
        }
    });
});

After checking the console log, you should be able to see the response data and determine why the success message is not being displayed correctly.
 

Similar threads

About this Thread

  • 1
    Replies
  • 252
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
992
Guests online
1,169
Total visitors
2,161

Forum statistics

Threads
2,273,853
Posts
28,952,081
Members
1,234,978
Latest member
panda1829
Back
Top