admin管理员组文章数量:1291009
I want to delete a DB record with php. Before I sumbit a form I want a notification of Sweet Alert (2) with the buttons "Yes of No".
When "yes" I want to submit the form and activate my php code.
Problem is it won't work. After I click "yes" my page is refreshing and my php code don't start.
<form action="" method="POST">
<div class="right gap-items-2">
<button class="btn btn-error" name="archive" type="submit" onclick="archiveFunction()">archive</button>
</div>
</form>
<script>
function archiveFunction() {
event.preventDefault(); // prevent form submit
var form = event.target.form; // storing the form
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.value) {
form.submit();
}
})
}
</script>
if (isset($_POST['archive'])){
$message = "test";
echo "<script type='text/javascript'>alert('$message');</script>";
}
I want to delete a DB record with php. Before I sumbit a form I want a notification of Sweet Alert (2) with the buttons "Yes of No".
When "yes" I want to submit the form and activate my php code.
Problem is it won't work. After I click "yes" my page is refreshing and my php code don't start.
<form action="" method="POST">
<div class="right gap-items-2">
<button class="btn btn-error" name="archive" type="submit" onclick="archiveFunction()">archive</button>
</div>
</form>
<script>
function archiveFunction() {
event.preventDefault(); // prevent form submit
var form = event.target.form; // storing the form
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.value) {
form.submit();
}
})
}
</script>
if (isset($_POST['archive'])){
$message = "test";
echo "<script type='text/javascript'>alert('$message');</script>";
}
Share
Improve this question
edited Mar 26, 2019 at 22:16
Sterling Archer
22.4k19 gold badges85 silver badges121 bronze badges
asked Mar 26, 2019 at 15:39
JessieJessie
291 gold badge1 silver badge3 bronze badges
0
4 Answers
Reset to default 4Modify the form button using an ID like this :
<form action="" method="POST">
<div class="right gap-items-2">
<button class="btn btn-error" name="archive" type="submit" id="submitForm" >archive</button>
</div>
</form>
And then the script like this :
<script>
$('#submitForm').on('click',function(e){
e.preventDefault();
var form = $(this).parents('form');
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.value) {
form.submit();
}
});
});
</script>
##php/file:##
<?php
$no = 1;
$sql = "SELECT * FROM office_info";
$query = $conn->query($sql);
while ($rows = $query->fetch_assoc()) {
echo
"<tr>
<td>" . $rows['id'] . "</td>
<td>" . $rows['office_name'] . "</td>
<td>" . $rows['office_type'] . "</td>
<td>" . $rows['office_address'] . "</td>
<td>" . $rows['office_cont_person'] . "</td>
<td>" . $rows['office_con_mobile_no'] . "</td>
<td>" . $rows['office_start_dt'] . "</td>
<td>" . $rows['office_end_dt'] . "</td>
<td>
<a href='office_info_edit.php?id=" . $rows['id'] . "' target='_blank' class='btn btn-success btn-sm><span class='glyphicon glyphicon-edit'></span>Edit</a>
<a href='office_info_delete.php?id=" . $rows['id'] . "' target='_blank' class='btn btn-success btn-sm><span class='glyphicon glyphicon-edit'></span>del</a>
<a data-id='" . $rows['id'] . "' id='delete_id' class='btn btn-danger btn-sm'<span class='glyphicon glyphicon-trash' href='javascript:void(0)'></span>delete</a>
</td>
</tr>";
}
?>
##script##
<script>
$(document).ready(function() {
$(document).on('click', '#delete_id', function(e) {
var id = $(this).data('id');
SwalDelete(id);
e.preventDefault();
});
});
function SwalDelete(id) {
Swal.fire({
title: 'Are you sure?',
text: "It will be deleted permanently!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
showLoaderOnConfirm: true,
preConfirm: function() {
return new Promise(function(resolve) {
$.ajax({
url: 'office_info_delete.php',
type: 'POST',
data: 'id=' + id,
dataType: 'json'
})
.done(function(response) {
Swal.fire('Deleted!', 'Your file has been deleted.', 'success')
})
.fail(function() {
Swal.fire('Oops...', 'Something went wrong with ajax !', 'error')
});
});
},
});
}
</script>
**office_info_delete.php**
if (isset($_POST['id'])) {
require '../database.php';
$id = intval($_POST['id']);
$deleteQuery = "delete from `office_info` where id='$id' limit 1";
$conn->query($deleteQuery);
if ($conn->affected_rows == 1) {
$response['status'] = 'success';
$response['message'] = 'Product Deleted Successfully ...';
} else {
$response['status'] = 'error';
$response['message'] = 'Unable to delete product ...';
}
echo json_encode($response);
}
$('#deleteamc').on('click',function(e) {
event.preventDefault();
var form = this;
swal({
title: "Are you sure?",
text: "All data related to this AMC ID will be parmanently deleted",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, DELETE it!",
cancelButtonText: "No, cancel please!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm){
if (isConfirm) {
form.submit();
} else {
swal("Cancelled", "AMC Record is safe :)", "error");
}
});
});
<link href="https://cdnjs.cloudflare./ajax/libs/sweetalert/1.1.3/sweetalert.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare./ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST" id="deleteform">
<input type="hidden" name="amccode" id="amccode" value="<?php echo $row['amccode']; ?>">
<button class="btn btn-danger" id="deleteamc" name="delete" type="submit">
<i class="fa fa-minus"></i>
Delete
</button>
</form>
<!-- USER CONFIRM DELETE THEN FIRE DELETE FUNCTION -->
<?php
if(isset($_REQUEST['delete']))
{
$amccode=$_POST['amccode'];
echo "WRITE DELETE SQL CODE";
exit;
");
?>
<script src="../source/alert/dist/sweetalert.min.js"></script>
<link rel="stylesheet" type="text/css" href="../source/alert/dist/sweetalert.css">
<script type="text/javascript">
function SuccessSaved (){
swal({
title: "Success",
text: "All Records Successfully Deleted. Customer ID <?php echo $amccode ?>",
type: "success",
},
function(){
window.location.href = "<?php echo BASE_URL; ?>/admin/amcall.php";
});
}
</script>
<script type="text/javascript" >
$( document ).ready(function() {
//console.log( "ready!" );
SuccessSaved();
});
</script>
<?php
}
?>
Try this one , I used this in laravel , It works
<td>
<form class="archiveItem"
action="{{route('archive.destroy',['id'=>$archive->id])}}"
method="Post">
{{method_field('DELETE')}}
{{csrf_field()}}
<input type="hidden" name="archive">
<a onclick="archiveRemove(this)" class="deleteRecord"
id="{{$archive->id}}"
style="cursor: pointer"><i class="fa fa-trash"
style="font-size: 20px;color: #000"></i>
</a>
</form>
and in scripts:
function archiveRemove(any) {
var click = $(any);
var id = click.attr("id");
swal.fire({
title: 'Are you sure !',
text: "?????",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'yes!',
cancelButtonText: 'no'
}).then(function (success) {
if (success) {
$('a[id="' + id + '"]').parents(".archiveItem").submit();
}
})
}
本文标签: javascriptDelete confirmation with sweet alert 2Stack Overflow
版权声明:本文标题:javascript - Delete confirmation with sweet alert 2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741506147a2382333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论