【问题标题】:Sweetalert2 Ajax - post input dataSweetalert2 Ajax - 发布输入数据
【发布时间】:2017-10-20 02:33:02
【问题描述】:

我最近在我的项目中使用 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 保存数据。

&lt;?php $CustomerKey; ?&gt;

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

任何帮助将不胜感激。

JSFiddle 示例;

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

【问题讨论】:

    标签: javascript jquery ajax sweetalert2


    【解决方案1】:

    您好,您需要在 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'];

    祝你好运

    【讨论】:

    • 如何将数据从 textarea 传递到 ajax?
    【解决方案2】:

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

    SweetAlert2 与 sweetalert 不同

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

    【讨论】:

      猜你喜欢
      • 2016-04-04
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多