4

我最近在我的项目中使用 SweetAlert2,我想整合一个“添加注释”功能。

用户单击一个按钮,被定向到一个页面,然后执行以下操作。

    <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从输入字段“textarea”发布数据。

我还想通过以下方式验证提交是成功还是失败

'成功'

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

“失败的”

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

我还需要将 PHP 对象传递给 ajax(唯一的客户 ID 键),并允许 ajax 保存数据。

<?php $CustomerKey; ?>

Sweet Alert 没有提供太多关于如何使用 ajax 的文档,并且很难找到与我的 stackoverflow 和在线搜索问题有关的更多信息。

任何帮助将不胜感激。

JSFiddle 示例;

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

4

2 回答 2

6

嗨,您需要做的是在 sweetalert 的 then 函数中进行 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"
    )
  }
})

在这个例子中,要在你的 php 文件中获取 customerKey 值,它的(YourPhpFile.php) 只需包括

$CustomerKey = $_POST['CustomerKey'];

祝你好运

于 2017-10-21T04:05:02.103 回答
0

首先我看到的是 SweetAlert2 所以我希望看到的是 swal.fire() 而不是 swal()

SweetAlert2 与 sweetalert 不同

SweetAlert2 文档:https ://sweetalert2.github.io/

于 2020-07-02T10:59:25.757 回答