How to ask the user a confirmation to delete object?

0

I have an inconvenience that when the user wants to delete a post it is deleted without further ado, but I want to ask the user some confirmation if he wants to delete it, I am working with php, eloquent, twige illuminate, for now I have this part in the controller of the get which happened a parameter that is the (id) of the post that is going to be deleted.

  

In this first part I have the administration index, from here I send the get for the elimination of post to the controller

{% 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 parameter of the id to be deleted

 public function getDelete($id){

        BlogPost::destroy($id);
        header('Location:' . BASE_URL . 'admin/posts');
    }
  

what I want to implement the confirmation 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>
    
asked by Asdrubal Hernandez 07.07.2018 в 16:41
source

2 answers

2

you can do it with js or with jquery using confirm and the id of the inputs.

you add an id to your button

<input class="btn btn-primary" type="submit" id="submit">

the script with jQuery would be

<script type='text/javascript' src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script type="text/javascript">
    $("#submit").click(function(){
    if(confirm("seguro de eliminar este post")){
     var id = $("#id").val();
     location.href= 'turuta/'+id+'/';
 }else{
            console.log('cancela la eliminada');
        }
 });

</script>

Tell me how you are doing

    
answered by 07.07.2018 / 17:54
source
1

You can use the code that happened to you @camilo and after the confirmation you call the function getDelete ($ id), something like that

<script type='text/javascript' 
 src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> 
</script>
<script type="text/javascript">
 $("#submit").click(function(){
    if(confirm("seguro de eliminar este post")){
        getDelete('#id');
    }else{
        console.log('cancela la eliminada');
    }
});
</script>
    
answered by 07.07.2018 в 18:10