javascript Sweetalert2 Ajax - 发布输入数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46841570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:58:04  来源:igfitidea点击:

Sweetalert2 Ajax - post input data

javascriptjqueryajaxsweetalert2

提问by lzoesch

I have recently been working with SweetAlert2 on my project, and I would like to put together a "Add Note" feature.

我最近在我的项目中与 SweetAlert2 合作,我想组合一个“添加注释”功能。

User clicks on a button, gets directed to a page, and the following executes.

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

    <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>

What I am trying to accomplish, and have a had a difficult time with is utilizing ajax to post the data from the inputfield "textarea".

我想要完成的,并且有一个困难的时间是利用 ajax 从输入字段“textarea”发布数据。

I would also like to validate that a submission was successful or failed by using the following

我还想通过使用以下内容来验证提交是成功还是失败

'Success'

'成功'

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

"Failed"

“失败的”

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

I also need to pass a PHP object to the ajax (a unique customer ID key), and allow ajax to save the data.

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

<?php $CustomerKey; ?>

<?php $CustomerKey; ?>

Sweet Alert doesn't give much documentation as to how to utilize ajax, and have had a difficult time finding more information pertaining to my problem with stackoverflow, and online searches.

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

Any help would be greatly appreciated.

任何帮助将不胜感激。

JSFiddle example;

JSFiddle 示例;

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

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

采纳答案by Hayden Passmore

Hi what you need to do is make your ajax call in the sweetalert's then function and pass the customer key variable as a POST variable using ajax's data parameter.

嗨,您需要做的是在 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"
    )
  }
})

And to get the customerKey value in your php file just include

并在您的 php 文件中获取 customerKey 值,只需包括

$CustomerKey = $_POST['CustomerKey '];

$CustomerKey = $_POST['CustomerKey '];

Good luck

祝你好运