What's new

Bat ganito

loktoy

Eternal Poster
Joined
Oct 20, 2014
Posts
1,168
Solutions
2
Reaction
103
Points
497
Age
30
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.
 
Back
Top