簡體   English   中英

刪除確認HREF Sweet Alert

[英]Delete confirmation HREF Sweet Alert

該鏈接可能重復

但是我似乎無法使用該論壇中的相同邏輯來解決此問題。

我正在開發一個預訂模塊,它具有確認取消預訂的功能。 這是我的PHP代碼,告訴您預訂是否已確認,取消或尚未確認。 Status==0 (尚未確認)-默認值, Status==1 (已確認)和Status==2 (已取消)

<?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])==0)
    {   
header('location:index.php');
}
else{
if(isset($_REQUEST['eid']))
    {
$eid=intval($_GET['eid']);
$status="2";
$sql = "UPDATE tblbooking SET Status=:status WHERE  id=:eid";
$query = $dbh->prepare($sql);
$query -> bindParam(':status',$status, PDO::PARAM_STR);
$query-> bindParam(':eid',$eid, PDO::PARAM_STR);
$query -> execute();

$msg="Booking Successfully Cancelled";
}

if(isset($_REQUEST['aeid']))
    {
$aeid=intval($_GET['aeid']);
$status=1;

$sql = "UPDATE tblbooking SET Status=:status WHERE  id=:aeid";
$query = $dbh->prepare($sql);
$query -> bindParam(':status',$status, PDO::PARAM_STR);
$query-> bindParam(':aeid',$aeid, PDO::PARAM_STR);
$query -> execute();

$msg="Booking Successfully Confirmed";
}

<?php $sql = "SELECT tblbooking.Status";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0){
  foreach($results as $result){
?>
if($result->Status==0){
echo htmlentities('Not Confirmed yet');
} else if ($result->Status==1) {
echo htmlentities('Confirmed');
}
else{
echo htmlentities('Cancelled');
}

這是原始代碼,將使用基本的Javascript警報來確認/取消預訂

<td><button class="btn btn-sm btn-primary"><a href="manage-bookings.php?aeid=<?php echo htmlentities($result->id);?>" onclick="return confirm('Do you really want to Confirm this booking')"> <i style="color: #fff;" class='fa fa-check'></i></a></button>

<button class="btn btn-sm btn-danger"><a href="manage-bookings.php?eid=<?php echo htmlentities($result->id);?>" onclick="return confirm('Do you really want to Cancel this booking')"> <i style="color: #fff;" class='fa fa-close'></i></a></button></td>

但是我想使用sweetalert,所以我對此進行了修改(首先使用錨進行測試)

<a href="manage-bookings.php?aeid=<?php echo htmlentities($result->id);?>" onclick="confirmation(event)">Confirm</a>

這是script

<script>
    function confirmation(ev) {
ev.preventDefault();
var urlToRedirect = ev.currentTarget.getAttribute('href'); //use currentTarget because the click may be on the nested i tag and not a tag causing the href to be empty
console.log(urlToRedirect); // verify if this is the right URL
swal({
  title: "Are you sure you want to tag this booking as confirmed",
  text: "You will not be able to revert this!",
  icon: "warning",
  buttons: true,
  dangerMode: true,
})
.then((willDelete) => {
  // redirect with javascript here as per your logic after showing the alert using the urlToRedirect value
  if (willDelete) {
    swal("Your booking as been confirmed!", {
      icon: "success",
    });
  } else {
    swal("You booking hasn't been confirmed.");
  }
});
}
</script>

問題是,警報起作用。 它會創建一個彈出確認,但它不會完全確認/取消預訂,盡管href鏈接與原始鏈接說的是相同的內容,但原始鏈接會重新加載頁面並進行標記。

在我看來,您實際上從未真正將任何數據發送回服務器。

內置的javascript確認對話框會中斷您的超鏈接,然后在您單擊“確定”后恢復它。

看起來Sweetalerts不會這樣做。 因此,您永遠不會重定向到需要訪問的頁面。 您可以使用javascript重定向到正確的URL,或者可以為要執行的操作編寫服務器端API,然后在“ willDelete”案例中從AJAX調用該API。

這是前一個選項:

<script>
    function confirmation(ev) {
        ev.preventDefault();
        var urlToRedirect = ev.currentTarget.getAttribute('href'); //use currentTarget because the click may be on the nested i tag and not a tag causing the href to be empty
        console.log(urlToRedirect); // verify if this is the right URL
        swal({
            title: "Are you sure you want to tag this booking as confirmed",
            text: "You will not be able to revert this!",
            icon: "warning",
            buttons: true,
            dangerMode: true,
        })
        .then((willDelete) => {
            if (willDelete) {
                // redirect with javascript here as per your logic after showing the alert using the urlToRedirect value
                window.location.href = urlToRedirect;
            } else {
                swal("You booking hasn't been confirmed.");
            }
        });
    }
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM