2017-10-20 46 views
1

私は最近SweetAlert2を自分のプロジェクトで使っていましたが、私は「ノートを追加」機能をまとめたいと思います。Sweetalert2 Ajax - 投稿入力データ

ユーザーがボタンをクリックしてページに誘導され、以下が実行されます。私が達成しようとしている、と入力フィールド「テキストエリア」からデータをポストするために、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> 

私はまた、提出が成功したか、使用して失敗したことを検証したいと思い、次の

「成功」

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を活用する方法についての多くのドキュメントを与えるものではありません、とstackoverflowのと私の問題に関する詳細については、オンライン検索を見つけるのは難しい時間を過ごしてきました。

ご協力いただければ幸いです。

JSFiddle例;

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

答えて

1

あなたが何をする必要があるかこんにちはsweetalertのその機能であなたのAJAX呼び出しを行うと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" 
    ) 
    } 
}) 

そして、ちょうど

$CustomerKey = $_POST['CustomerKey '];

幸運

を含めるあなたのphpファイルでcustomerKey値を取得します