Are you sure want to delete? - javascript

I have on my delete function a message for confirmation of delete when i put ok nothing is done
Function from controller
public function destroy($id)
{
$client =client::find($id);
$client->delete();
return redirect('client');
}
view client
<form action="{{url ('client/'.$client->id)}}" method="post">
{{csrf_field()}}
{{method_field('DELETE')}}
<a href="{{url('client/'.$client->id.'/show')}}"
class="btn btn-default btn-sm">Details</a>
<a href="{{url('client/'.$client->id.'/edit')}}"
class="btn btn-primary btn-sm">Editer</a>
<a class="btn btn-danger btn-sm" title="Delete"
href="javascript:if(confirm('Are you sure want to
delete?')) $('#frm_{{$client->id}}').submit()">
Supprimer
</a>
</form>

in your route file just change like this
Route::get('/client/{id}/delete', 'ClientController#destroy');
in your view
Delete

$('#frm_{{$client->id}}').submit() is a jQuery code, are you sure your page have jQuery properly loaded?
Better use plain javascript: document.getElementById("myForm").submit()

i think you forget to add id attribute for you form
<form action="{{url ('client/'.$client->id)}}" id="frm_{{$client->id}}" method="DELETE">

This can be done easily by:
<a onclick="if(confirm('Are you sure you want to delete this item?')){ deleteFunction() };">Delete</a>

Related

Laravel, call Delete with JavaScript

I'm still a beginner with Laravel. I wanted to do the usual "are you sure you want to delete this" question as a modal. I'm trying to do it with JS, and I have the following function that I found on this very website.
function confirmClick(){
let url = "{{route('comics.destroy', ':id') }}";
url = url.replace(':id', idComic);
window.location.href = url;
};
But when I use this what I get is the show route. This is what I have in my Controller
public function destroy($id)
{
$comic = Comic::findOrFail($id);
$comic->delete();
return redirect()->route('comics.index');
}
And the html
<div class="hidden" id="deleteModal">
<h2>Sei sicuro di volerlo eliminare?</h2>
<button type="submit" class="btn btn-danger" id="yesBtn">Sì</button>
<button type="submit" class="btn btn-info" id="noBtn">No</button>
</div>
Do i have to add in some way a method and the token? In case I have to, how do I do this? Sorry if this might look confusing, but it comes from a really confused mind at the moment lol
I suggest you use a different approach, mostly because Laravel's default route for DELETE requieres a POST.
This is a code I use very often. You have a form with the method and token, and a button with a class "deletebutton".
If you click the button, it triggers the "confirmation" from this library https://bootstrap-confirmation.js.org/. You can use another library, or use your own implementation returning a true/false value to evaluate and then decide if you submit the form or not.
if the user confirms, it looks for the form and then triggers the "submit"
<form class="form_delete" action="/record/{{ $record->id }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="button" class="btn btn-sm btn-danger deletebutton"><i class="fa fa-trash"></i>
</button>
</form>
<script>
$(document).ready(function () {
$('.deletebutton').confirmation({
rootSelector: this,
container: 'body',
popout: true,
placement: 'top',
singleton: true,
onCancel: function () {
},
onConfirm: function (value) {
var myform = $(this).closest(".form_delete");
myform.submit();
}
});
});
</script>

Datatable Action Custom Delete Confirmation not working

Actually am trying to implement custom actions buttons (In my case using a tag) in my datatable. here i want when user click on delete link then i want a confirm box saying "Are you sure" or something like that.
public function dataTable($query)
{
$editUrl = route('signup.index');
// <a class="btn btn-info waves-effect" href="'.$editUrl.'/{{$id}}/delete">Delete1</a>
if(Auth::user()->hasRole('Super Admin'))
return datatables()
->eloquent($query)
->addColumn('action', '<a class="btn btn-info waves-effect" href="'.$editUrl.'/{{$id}}/delete" onclick="return confirm(1)">Delete</a>');
else
return datatables()
->eloquent($query);
}
Now Here when i click on delete link then it is working fine But when i pass any string in confirm function then the confimation not working.
public function dataTable($query)
{
$editUrl = route('signup.index');
// <a class="btn btn-info waves-effect" href="'.$editUrl.'/{{$id}}/delete">Delete1</a>
if(Auth::user()->hasRole('Super Admin'))
return datatables()
->eloquent($query)
->addColumn('action', '<a class="btn btn-info waves-effect" href="'.$editUrl.'/{{$id}}/delete" onclick="return confirm("Are you Sure")">Delete</a>') //This is not working;
else
return datatables()
->eloquent($query);
}
is there any other approach for this, thanx in advance.
You have a double quote " issue at onclick="return confirm("Are you Sure")". Just using another type of quote for the string then it will work as expected. Something like:
onclick="return confirm(`Are you Sure`)"

How to stop next page to be loading

In my project, while a particular button is clicked I want to stop the next page appearing. Here is my JavaScript code:
function checkcond() {
check_value=document.getElementById("chkvalue");
if(check_value==null){
alert(check_value);
document.firstform.textview.focus();
return false;
}
}
and button code is:
<form id="payment_form" name="payment_form" action="<?php echo site_url("cont/accept");?>" method="post">
<input id="chkvalue" name="chkvalue" type="hidden">
<button type="submit" id="submit_button" class="btn btn-primary" onclick="checkcond()">
<b>Make a Payment</b>
<span class="fa fa-hand-o-right" aria-hidden="true"></span>
</button>
Here after checking the check_value I want to keep my current page while it is null. How should it be done? Is there any function for that?
My suggestion would be to remove inline javascript
and use like this
document.getElementById('payment_form').onsubmit = function() {
return checkcond();
};
or if you want to use inline method, change onclick method like this
<button type="submit" id="submit_button" class="btn btn-primary" onclick="return checkcond()"><b>Make a Payment</b>

Calling controller AngularJS from a button in HTML

I need to call the controller deleteMember so to when the user clicks on a button, the member is deleted.
//deleting a member
Members.controller('deleteMember',['$scope','$http',function($scope, $http){
$scope.deleteMember = function(member){
$scope.deleteMember="";
console.log(member);
var deleteMember=confirm("Sure you want to delete?");
if(deleteMember){
$http.post('PHP/deleteMember.php',member).success(
function(data){
console.log(data);
if (data){
console.log("Deletion successful"); //delete worked
}else{
console.log("Deletion not successful"); //delete did not work
}
});
};
};
}]);
HTML code:
<div class="col-md-2">
<td><button type="button" class="btn btn-warning">Delete</button><td> <!--on button click, the member will be deleted-->
</div>
Is there a way that I can write the name of the controller using HTML?
Thanks for the help :)
You could add the ng-click attribute to the button:
<button type="button" ng-click="deleteMember(member)" class="btn btn-warning">Delete</button>
In this example I assume that the member variable that is passed to the deleteMember method is already in the scope of this button. This would be the case if this button is rendered inside an ng-repeat directive.
For example:
<tr ng-repeat="member in members">
...
<td>
<button type="button" ng-click="deleteMember(member)" class="btn btn-warning">
Delete
</button>
<td>
</tr>
Also you should probably not shooting yourself into the foot by replacing the deleteMember function with a string because the next time you want to call this method it simply won't work:
$scope.deleteMember = "";
You can simply just call the function deleteMember using ng-click and make sure you pass in the member you want to delete as an argument. You are passing a member in your function as an argument in the controller, so one must also be passed via the HTML. You haven't shown it in your code but I assume you are using ng-repeat to do this.
<div class="col-md-2">
<td><button type="button" class="btn btn-warning" ng-click="deleteMember(member)">Delete</button><td> <!--on button click, the member will be deleted-->
</div>
You mention calling the controller in the HTML. If you mean your page isn't initiated with the 'deleteMember' controller then you can wrap that block of code using ng-controller to give you access to the deleteMember.
<div class="col-md-2" ng-controller="deleteMember">
<td><button type="button" class="btn btn-warning" ng-click="deleteMember(member)">Delete</button><td> <!--on button click, the member will be deleted-->
</div>

Customize the cancel code button of the X-editable (AngularJS)

Is that possible when the user add a new row and by clicking on the cancel button(without put any data), the row will be deleted.
Otherwise how can I change the cancel button code, because this one use the default xeditable code of angularJS.(Or maybe how can I call the delete function if the row is empty?)
This is the EXAMPLE.
HTML for the cancel button:
<button type="button" ng-disabled="rowform.$waiting" ng-click="rowform.$cancel()" class="btn btn-default">
cancel
</button>
You may call your own function. To achieve this you should change your html like this:
<button type="button" ng-disabled="rowform.$waiting"
ng-click="cancelAdvice(rowform, $index)"
class="btn btn-default">
cancel
</button>
As you can see there is a new function with the form and the current index as parameter. In your controller you have to define this function:
$scope.cancelAdvice = function(rowform, index){
console.log(rowform, index);
$scope.removeUser(index);
rowform.$cancel();
}
Now you can do your own stuff and call the form $cancel if you are done.
Alternatively if you look at xeditable.js you'll see that $cancel() internally calls $oncancel() which looks for oncancel attribute on the form and calls the function supplied in it. So instead of handling the form in the controller you could have:
<form editable-form name="rowform" onbeforesave="saveRole($data, $index)" oncancel="removeIfNewRow($index)" ng-show="rowform.$visible" class="form-inline" shown="inserted == role">
<button type="submit" ng-disabled="rowform.$waiting" class="btn btn-primary">
save
</button>
<button type="button" ng-disabled="rowform.$waiting" ng-click="rowform.$cancel()" class="btn btn-default">
cancel
</button>
</form>

Categories