I want to create a confirmation dialog in JavaScript before deleting an item.
My JavaScript code
function confirmerSuppression() {
var confirmation = confirm("Êtes-vous sûr de vouloir supprimer cet élément ?");
if (confirmation) {
window.location.href = '/tiers.destroy/' + id; //
} else {
alert("Suppression annulée !");
}
}
Code for my delete button
<form action="{{route('tiers.destroy', $tiers->id)}}" method="POST">
@csrf
<button onclick="confirmerSuppression()" class="btn btn-danger w-100 type="submit">DELETE</button>
</form>
My route:
Route::post('/tiers/details/{id}', [AppHttpControllerstiersController::class, 'destroy'])->name('tiers.destroy');
My controller :
public function destroy($id)
{
Tier::where("id","=",$id)->delete();
return redirect()->route('prospects.liste')->with("success","Tier supprimée avec succès.");
}
When I click on the DELETE button the dialog appears and when I click on Cancel it deletes the element the same when I click on Yes.
Anyone have an idea please.