Ah Hao
Ah Hao

Reputation: 5

delete + js + laravel

<script>
$(document).ready(function() {
$.ajaxSetup({
  headers:
  {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
  });
  });
      //For delete function <button onclick="deleteDog">
function deleteSales(url) {
  $.ajax({
    type: "POST",
    url: url,
    success: function(result) {
    location.reload();
    }
    });
  }
</script>

**popup click yes and error**
<p>THIS NOT WORK</p>
Only popup when click ok then error

<button onclick="return confirm('Are you sure?')" href="deleteSales('{{ route('sales.delete', ['id' => $value->sales_id]) }}')"></button>

Upvotes: 0

Views: 1122

Answers (1)

Nate
Nate

Reputation: 2404

A button does not have a href attribute. You need to put both the confirm and the ajax call all into the onclick function. So, I moved the confirm into the deleteSales function. confirm returns a Boolean about whether the user accepted or not.

I don’t know PHP, so I can’t confirm if that url is being generated correctly or not, but everything else should work just fine.

<script>
  $(document).ready(function() {
    $.ajaxSetup({
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    });
  });

  function deleteSales(url) {
    if(confirm('Are you sure?')) {
      $.ajax({
        type: "POST",
        url: url,
        success: function(result) {
          location.reload();
        }
      });
    }
  }
</script>

<button onclick="deleteSales('{{ route('sales.delete', ['id' => $value->sales_id]) }}')"></button>

Upvotes: 1

Related Questions