user5534204
user5534204

Reputation:

delete using javascript in Laravel

i want to delete records with alert messages using javscript on my laravel view without refreshing the page.

my current code

 var dell = "Are you sure you want to delete ?";
var del_url = "{{url('destroy-shift')}}/" + json_obj[i].id;
                                                output +=

                                                        "<td></td>" +
                                                        "<td></td>" +
                                                        "<td>" + json_obj[i].START + "</td>" +
                                                        "<td>" + json_obj[i].END + "</td>" +
                                                        "<td>" + res + "</td>" +
                                                        "<td>" + json_obj[i].RUN + "</td>" +
                                                        "<td><a data-toggle='modal'  data-target='#myModal3"+xx+"' href='" + url + "' ><span class='glyphicon glyphicon-pencil '></span></a>" +
                                                        "<div class='modal fade' id='myModal3"+xx+"' role='dialog'>" +
                                                        "<div class='modal-dialog'>" +
                                                        "<div class='modal-content'><div class='modal-body'></div>" +
                                                        "</div>" +
                                                        "</div>" +
                                                        "</div>" +
                                                        "<a href='" + del_url + "' ><button type='submit' id='delete-btn' onclick='return confirm('" + dell + "')'><span class='glyphicon glyphicon-trash'></span></button></a></td>";


                                            output += "</tr>";

but still page is refreshing and im not getting the confirmation message too. plse advice

Upvotes: 0

Views: 1086

Answers (1)

MasterSith
MasterSith

Reputation: 173

Use ajax call. Simply make a button and on click do the ajax call. Then in Laravel do what you want, detele, update etc...

    $('#Yourbutton').click(function() {
        $.ajax({
            url: "YourUrl",
            type: 'POST',
            success: function(){
                alert("success");
            }
        });
    });

YourUrl is url where you call your post method, so make path in your routes.php like

Route::POST('YourUrl', 'Somecontroller@Yourmethod');

Upvotes: 1

Related Questions