PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 19, 2022

[FIXED] How to send a DELETE request from Button with JavaScrpt?

 April 19, 2022     javascript, laravel, laravel-8, php     No comments   

Issue

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.


Solution

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.



Answered By - Pádraig Galvin
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing