Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

100
Visualizações
How to send a DELETE request from Button with JavaScrpt?

I am using the resource controller in laravel 8 and I want to hit its delete method from a delete button. How can I create a button with methord='delete'. I have to create this in JavaScript as the button is in a table and I have created that table with JS.

my Edit button is like this:

<a href='"+base_url+"/donner/"+user['donner_id']+"/edit'><button class='btn btn-sm btn-outline-warning'><i class='fa-solid fa-pen-to-square'></i>&nbsp;&nbsp;Edit</button></a>

For the delete button, I have to send a request with the delete method. form buttonor anchor tag.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Take a look on the example code below. It shows you what are the steps. Bind a js trigger to the button. Then use fetch or axios or whatEverToMakeJSApiCalls.

Note Im sure that the next problem will come. CSRF, etc

<button 
  class=""
  onclick="deleteSomething( {{$id}} )">
</button>
function deleteSomething(id) {
  fetch('/api/deleteModelName/' + id,  {
    method: 'DELETE'
  })
}
about 4 years ago · Juan Pablo Isaza Relatório

0

It's not possible to send a DELETE request directly from pure HTML. You can either use JavaScript to send the DELETE request, or some frameworks support passing the method as a parameter in a POST request body.

<form action="/delete" method="POST">
    <input type="hidden" name="_method" value="DELETE" />
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <input type="submit" value="Delete">
</form>

This technique uses a form who's only visible input is a button. When the button is clicked it submits the form sending a POST request to the server. The server then detects the "_method" field and treats it as a DELETE request.

You can learn more about method spoofing in the Laravel docs.

about 4 years ago · Juan Pablo Isaza Relatório

0

<a onclick=&nbsp;sendDelete('"+base_url+"/donner/"+user['donner_id']+"/delete')&nbsp;><button class='btn btn-sm btn-outline-warning'><i class='fa-solid fa-pen-to-square'></i>&nbsp;&nbsp;Delete</button></a>
function sendDelete(p) {
  var xhr = new XMLHttpRequest();
  xhr.open("DELETE",p);
  xhr.send();
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda