Sweetalert package and jQuery submit() not working correctly

1.2k views Asked by At

Using sweetalert library. Here's my form:

<form id="delete-currency" style="display:inline;" method="POST" action="/admin/currency/7">
   <input type="hidden" name="_token" value="sIgpTKlPJ4Z3Co4daRwGpZ8rz10TCM6Ynre8sdsdsd">
   <input type="hidden" name="_method" value="DELETE">
   <button type="button" class="btn btn-danger btn-delete"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
</form>

7 - is an item id.

Then i am using confirm window (sweetalert):

<script type="text/javascript">
    $('.btn-delete').click(function() {
        swal({
            title: "Are you sure?",
            text: "blablabla!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes, i am sure",
            closeOnConfirm: false,
            cancelButtonText: "Cancel"
        },
        function(isConfirm) {
            $('#delete-currency').submit();
        });
    });

</script>

I have a resource controller, which accepts delete method with currency id. However if i use sweetalert with jquery submit() it always sends "1" instead of actual $currency->id

My controller:

public function destroy($id)
{
    dd($id);
    $currency = Currency::findOrFail($id);
    $currency->delete();
    alert()->success('asdasdsadad', 'Success')->persistent('close');
    return redirect('/admin/currency');
}

dd($id) shows "1" all the time.

routes.php:

Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function() {
    Route::resource('currency', 'CurrencyController');
});

What's wrong?

1

There are 1 answers

18
Kgn-web On BEST ANSWER

Try this.

 $('.btn-delete').on('click',function(e){
    e.preventDefault();
    var form = $(this).parents('form');
swal({
    title: "Are you sure?",
     text: "blablabla!",
     type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    closeOnConfirm: false
}, function(isConfirm){
    if (isConfirm) form.submit();
});

})

I think this will give you the right value.

Hope it helps