How to show a confirm message before delete?
Write this in onclick
event of the button:
var result = confirm("Want to delete?");
if (result) {
//Logic to delete the item
}
delete confirmation in laravel
I prefer a more easier way, just add onclick="return confirm('Are you sure?')"
, as bellow:
<a class="btn btn-danger" onclick="return confirm('Are you sure?')" href="{{route('city-delete', $result->my_id)}}"><i class="fa fa-trash"></i></a>
How to add confirmation message before deleting?
You can use the built-in method confirm()
in your deleteUser()
function. Please see below code,
function deleteUser() {
if(confirm('Are you sure want to delete user?')){
var rowId =
event.target.parentNode.parentNode.id;
//this gives id of tr whose button was clicked
var data =
document.getElementById(rowId).querySelectorAll(".row-data");
/*returns array of all elements with
"row-data" class within the row with given id*/
var uID = data[0].innerHTML;
document.getElementById("toBeEdit").value = uID;
document.getElementById("taskStatus").value = 'delete';
//alert("ID: " + uID);
const form = document.getElementById('editForm');
form.submit();
}
}
The confirm()
method displays a dialog box with a message, an OK button, and a Cancel button.
The confirm()
method returns true if the user clicked "OK", otherwise false.
javascript delete confirmation before deleting
You must use the return value of the confirm dialog:
echo"<form method = \"post\" action =\"change.php?change=$productid\">";
echo// form fields here!...
echo"<input type=\"submit\" name = \"delete\" value=\"Delete\" onclick=\"return deleletconfig()\" />";
if (isset($_POST['delete'])){ //delete clicked
//get variables here
//run query delete record from xyz where id=$id
}
<script>
function deleletconfig(){
var del=confirm("Are you sure you want to delete this record?");
if (del==true){
alert ("record deleted")
}else{
alert("Record Not Deleted")
}
return del;
}
</script>
See the changes in "onclick" and "deleletconfig".
confirmation message before deleting an item from cart
Yes, you can show confirm dialog before deleting item from cart. By default core.js
and theme.js
file handles all events and update cart accordingly on updateCart
event. (Refer more on events here)
To overcome default behaviour adding js
prior to theme.js
will help us to prevent default click event. Follow below mentioned step by step guide to load you own js
and add confirmation dialog on item delete.
1) Register your js
in theme.yml
(More details here) by adding below code under assets
themes/{your_theme}/config/theme.yml
assets:
js:
cart:
- id: cart-extra-lib
path: assets/js/cart-lib.js
priority: 30
2) Create file cart-lib.js
under themes/{your_theme}/assets/js
and add below code into it.
themes/{your_theme}/assets/js/cart-lib.js
function refreshDataLinkAction() {
$('[data-link-action="delete-from-cart"]').each(function(){
$(this).attr('data-link-action', 'confirm-remove-item');
});
}
$(document).on('click', '[data-link-action="confirm-remove-item"]', function(e) {
e.preventDefault();
if (confirm('Are you sure you want to remove product from cart?')) {
$(this).attr('data-link-action', 'delete-from-cart');
$(this).trigger('click');
}
return false;
});
$(document).ready(function () {
refreshDataLinkAction();
prestashop.on('updatedCart', function (event) {
refreshDataLinkAction();
});
});
3) Now, to load your js file you need to delete file config/themes/{your_theme}/shop1.json
(Reference)
4) Add products to cart and check cart; delete items you will see confirmation message. Attaching image for reference.
Confirmation Before Delete
A simple way is add a confirm and test the response
function delete_records()
{
var conf= confirm("Do you really want delete?");
if (conf== true){
document.frm.action = "delete.php";
document.frm.submit();
}else{
return;
}
}
Show confirmation dialog before deleting a p:dataTable entry
There is an easier way to do this, by using the p:confirmDialog
. This allows you to simply add p:confirm
to your p:commandButton
and you're done:
<h:form>
<p:dataTable var="var" value="#{bean.list}">
<p:column id="id">
<p:commandButton id="deleteButton"
action="#{bean.deleteRowAction(var)}">
<p:confirm header="Confirmation"
message="Are you sure?"
icon="pi pi-exclamation-triangle" />
</p:commandButton>
</p:column>
</p:dataTable>
<p:confirmDialog global="true">
<p:commandButton value="Yes" type="button"
styleClass="ui-confirmdialog-yes" icon="pi pi-check" />
<p:commandButton value="No" type="button"
styleClass="ui-confirmdialog-no" icon="pi pi-times" />
</p:confirmDialog>
</h:form>
Blazor - show confirmation dialog before delete/update?
@inject IJSRuntime JsRuntime
<tbody>
...
</tbody>
@code {
async Task DeleteSymbol(string id)
{
bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Are you sure?");
if (confirmed)
{
// Delete!
}
}
}
Sweet Alert confirmation before delete
As other mentioned, your button click is submitting the form to the specified action and you are not able to chose your option in the alert. So you should prevent the form submit by using event.preventDefault()
and submitting the form only when user press yes.
function archiveFunction() {event.preventDefault(); // prevent form submitvar form = event.target.form; // storing the form swal({ title: "Are you sure?", text: "But you will still be able to retrieve this file.", type: "warning", showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "Yes, archive it!", cancelButtonText: "No, cancel please!", closeOnConfirm: false, closeOnCancel: false},function(isConfirm){ if (isConfirm) { form.submit(); // submitting the form when user press yes } else { swal("Cancelled", "Your imaginary file is safe :)", "error"); }});}
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css"><form action="abc" method="POST"> <input type="hidden" name="p_id" id="p_id" value="<?php echo $rows['p_id']; ?>"> <button class="btn btn-danger" name="archive" type="submit" onclick="archiveFunction()"> <i class="fa fa-archive"></i> Archive </button></form>
Related Topics
Reset Form Input Values in React
How to Add Pm or Am Based on Hour from Input
Getting the Reason Why Websockets Closed With Close Code 1006
How to Count Text Lines Inside an Dom Element Can I
How to Filter Date Range in Datatables
Background-Image in React Component
How to Convert from Google Sheet Date Number Value to JavaScript Date
Call External JavaScript Function from React Typescript Components
How to Pass JavaScript Confirm Variable to PHP Variable
Retrieving Json Response Data With Dynamic Key
Javascript - Remove Milliseconds from Date Object
Looping Through a Json Object Containing More Objects and Arrays
React Passing Import Name as Prop and Adding to Image Src
Check If a Number Has Repeated Digits
Escaping Apostrophe (Single Quote) Character in JavaScript and HTML
Executing Nodejs Script File in PHP Using Exec()