Skip to content
Advertisement

How can i make if else condition inside sweetalert2 swal.fire?

My table has relationship with other field table(Constraint foreign key), so when this delete button click. it will show sweet alert that the data cannot be delete if they connected.

Here my code

<tbody>
                                            <?php
                                        $no = 1;
                                        foreach ($data as $row){?>
                                            <tr id="<?php echo $row->id; ?>">
                                                <th scope="row"><?php echo $no ?></th>
                                                <td><?php echo $row->id?></td>
                                                <td><?php echo $row->name?></td>
                                                <td>
                                                    <button type="submit"
                                                        class="btn btn-icon btn-flat-danger mb-1 remove-department"
                                                        data-toggle="tooltip" data-placement="top" title="Delete"><i
                                                            class="feather icon-trash-2"></i></button>
                                                </td>
                                            </tr>
                                            <?php $no++; } ?>
                                        </tbody>

and here my sweetalert.js :

$(".remove-department").click(function () {
var id= $(this).parents("tr").attr("id");
Swal.fire({
    title: 'Are you sure?',
    text: "You won't be able to revert this!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, delete it!'
}).then((result) => {
    if (result.value) {
        $.ajax({
            url: "/ci/app/admin/department-delete/" + id,
            method: "DELETE",
            success: function () {
                Swal.fire(
                    'DELETE',
                    'SUCCESSFULLY DELETED',
                    'success',
                ).then(function () {
                    location.href = "/ci/app/admin/department";
                });
            },
        })
    } else if (result.dismiss === swal.DismissReason.cancel) {
        swal.fire(
            'Canceled',
            'Your data not deleted',
            'error'
        )
    }
})

});

how can i make if else condition inside the sweetalert ? For example : i have department name called “Head Management” in this table, the head management is being used in other table, so when user wanna delette this Head Management, the system will show error with sweet alert. Sorry for my bad english.

$.ajax({
            url: "/ci/app/admin/department-delete/" + id,
            method: "DELETE",
            success: function () {
              if( ....  ==TRUE){
                Swal.fire(
                    'DELETE',
                    'SUCCESSFULLY DELETED',
                    'success',
                ).then(function () {
                    location.href = "/ci/app/admin/department";
                });
              }else{
                 Swal.fire(
                    'Error',
                    'Fail DELETED',
                    'error',
            },
        })

Advertisement

Answer

you can use the catch error from ajax method, It´s like:

$.ajax({
        url: "/ci/app/admin/department-delete/" + id,
        method: "DELETE",
        success: function () {
            //Message ok
          },
          error: function(){
            //Failure message
            },
        })

So you have to set your url with query to response according with it happened

Other way is make an echo in your url with query and getting in your success, like this:

 $.ajax({
            url: "/ci/app/admin/department-delete/" + id,
            method: "DELETE",
            success: function (response) {
                if(response){//successful validation
                //Message ok
                }else{
                //Failure message 
              }
            })

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement