2017-10-20 189 views
1

我最近一直在使用SweetAlert2在我的項目上,並且我想組合一個「添加註釋」功能。Sweetalert2 Ajax - 後輸入數據

用戶單擊一個按鈕,定向到一個頁面,並執行下列操作。

<script>swal({ 
     title: "Add Note", 
     input: "textarea", 
     showCancelButton: true, 
     confirmButtonColor: "#1FAB45", 
     confirmButtonText: "Save", 
     cancelButtonText: "Cancel", 
     buttonsStyling: true 
    }).then(function() { 
     swal(
     "Sccess!", 
     "Your note has been saved!", 
     "success" 
    ) 
    }, function (dismiss) { 
     if (dismiss === "cancel") { 
     swal(
      "Cancelled", 
"Canceled Note", 
      "error" 
     ) 
     } 
    })</script> 

我試圖完成,並有一個有困難的時候與被利用AJAX後從inputfield「文本區域」的數據。

我也想驗證一個提交成功或使用不合格以下

「成功」

swal(
     "Sccess!", 
     "Your note has been saved!", 
     "success" 
    ) 

「失敗」

swal(
      "Internal Error", 
      "Oops, your note was not saved." 
      "error" 
     ) 

我還需要通過一個PHP對象給ajax(唯一的客戶ID密鑰),並允許ajax保存數據。

<?php $CustomerKey; ?>

甜警報並沒有給太多的文件以如何利用Ajax和有困難的時候找到屬於我的問題,計算器,和在線搜索的詳細信息。

任何幫助將不勝感激。

JSFiddle example;

https://jsfiddle.net/px0e3Lct/1/

回答

1

嗨,你需要做的是讓你的Ajax調用在sweetalert當時的功能和使用AJAX的數據參數傳遞客戶的關鍵變量作爲POST變量。

var CustomerKey = 1234;//your customer key value. 
swal({ 
    title: "Add Note", 
    input: "textarea", 
    showCancelButton: true, 
    confirmButtonColor: "#1FAB45", 
    confirmButtonText: "Save", 
    cancelButtonText: "Cancel", 
    buttonsStyling: true 
}).then(function() {  
    $.ajax({ 
     type: "POST", 
     url: "YourPhpFile.php", 
     data: { 'CustomerKey': CustomerKey}, 
     cache: false, 
     success: function(response) { 
      swal(
      "Sccess!", 
      "Your note has been saved!", 
      "success" 
      ) 
     } 
     failure: function (response) { 
      swal(
      "Internal Error", 
      "Oops, your note was not saved.", // had a missing comma 
      "error" 
      ) 
     } 
    }); 
}, 
function (dismiss) { 
    if (dismiss === "cancel") { 
    swal(
     "Cancelled", 
     "Canceled Note", 
     "error" 
    ) 
    } 
}) 

並獲得在你的PHP文件中的customerKey值只包括

$CustomerKey = $_POST['CustomerKey '];

好運