I have a problem that when the user wants to delete a post it is deleted without further ado, but I want to ask the user for some confirmation if he wants to delete it, I am working with php, eloquent, twige illuminate, for now I have this part in the get controller which pass a parameter that is the (id) of the post to be deleted.
In this first part I have the administration index, from here I send the get to the controller to delete the post
{% extends "layout.twig" %}
{% block content %}
<a class="btn btn-primary" style="margin-bottom: 10px" href="{{ 'admin/posts' | url }}">Back</a>
<form action="{{ 'admin/posts/update' | url }}" method="post">
{% if errors %}
{% include ('partials/errors.twig') %}
{% endif %}
{% if result %}
<div class="alert alert-success">
Success!!!
</div>
{% endif %}
<div class="form-group">
<h2>{{ blogPost.title }}</h2>
</div>
<div class="form-group">
<label for="inputImg">Image</label>
<input class="form-control" type="text" name="img" id="inputImg" value="{{ blogPost.img_url }}">
</div>
<input type="hidden" name="id" value="{{ blogPost.id }}">
<div class="form-group">
<label for="inputContent">Content</label>
<textarea class="form-control" name="content" id="inputContent" rows="10">{{ blogPost.content }}</textarea>
</div>
<div class="form-group">
<input class="btn btn-primary" style="margin-top: 10px" type="submit" value="Save">
</div>
</form>
{% endblock %}
Here I have the controller which receives the id parameter to be deleted
public function getDelete($id){
BlogPost::destroy($id);
header('Location:' . BASE_URL . 'admin/posts');
}
What I want to implement the confirmation to is an anchor, delete:
<tbody>
{% for blogPost in blogPosts %}
<tr>
<td>{{ blogPost.title }}</td>
<td><a class="btn btn-warning" href="{{ "admin/posts/update/" | url }}{{ blogPost.id }}">Edit</a></td>
<td><a class="btn btn-danger" href="{{ 'admin/posts/delete/' | url }}{{ blogPost.id }}">Delete</a></td>
</tr>
{% endfor %}
</tbody>
You can do it with js or with jquery using confirm and the id of the inputs.
you add an id to your button
the script with jQuery would be
tell me how it goes
You can use the code that @camilo gave you and after the confirmation you call the getDelete($id) function, something like this