1

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?

Alexander Kim
  • 17,304
  • 23
  • 100
  • 157

1 Answers1

2

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

Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • Your line `//post the form` - you mean to post with AJAX? If yes, i don't need ajax there. Also with your code my delete request would be submitting on the page load, i don't need that. I want it to fire when a user press "DELETE" button. `currency:258 Uncaught SyntaxError: Unexpected token !` So it's impossible to do with `click()` ? – Alexander Kim Dec 19 '16 at 11:19
  • updated my post to meet your requirement. Please check :) – Kgn-web Dec 19 '16 at 11:21
  • Not working, complains about token. Why is it a bad idea using `click()` ? – Alexander Kim Dec 19 '16 at 11:31
  • http://stackoverflow.com/questions/12785241/difference-between-handling-form-submit-or-an-input-type-submit-click-e – Kgn-web Dec 19 '16 at 11:32
  • Can you try one more update instead of passing id in the url, Use hidden field & pass it to server. It will surely fix the issue ` ` – Kgn-web Dec 19 '16 at 11:35
  • currency:258 Uncaught SyntaxError: Unexpected token ! This is the actual cause. You should look into it & get it fixed. That's it – Kgn-web Dec 19 '16 at 11:39
  • It's actually complains about your code: https://yadi.sk/i/Nfdoug3U343JkE – Alexander Kim Dec 19 '16 at 11:50
  • Lol, Please check what I did. I am using !isConfirm flag inside if condition not in the function parameter – Kgn-web Dec 19 '16 at 11:54
  • Now it complains about Unexpected token "if" ... wtf... https://yadi.sk/i/sED9i9m_343N4Q What i am doing wrong? – Alexander Kim Dec 19 '16 at 11:58
  • I have corrected my snippet, swal() should be closed & then if condition should be checked – Kgn-web Dec 19 '16 at 12:00
  • No errors now, but clicking on my submit button does nothing now. *cking sweetalert... – Alexander Kim Dec 19 '16 at 12:04
  • Change button type to submit ` – Kgn-web Dec 19 '16 at 12:15
  • if i change to submit type, then sweet alert not firing its modal window. If i'm not using Sweetalert, then everything works as expected. – Alexander Kim Dec 19 '16 at 12:16
  • Why you need Sweetalert if you are reloading the page. Surely with submit button it will not pop-up the sweet alert. It its ajax call then swal makes sense. – Kgn-web Dec 19 '16 at 12:18
  • i'll explain now. It's an admin panel with lists of all my pages. Take a look at it: https://yadi.sk/i/qvR6YCLl343XbG hope you'll get my idea. – Alexander Kim Dec 19 '16 at 12:21
  • I have updated my snippet. Check it. Plus,this link would be helpful to you http://stackoverflow.com/questions/32164151/sweet-alert-continue-submitting-form-on-confirm – Kgn-web Dec 19 '16 at 12:28
  • works now! Thank you very much. You didn't got my idea at the start? – Alexander Kim Dec 19 '16 at 12:34
  • Yes.. In your screenshot I got to know what you are trying to do. But glad that we fixed it Cheers :) – Kgn-web Dec 19 '16 at 12:43