Sweetalert2 Ajax - 发布输入数据 [英] Sweetalert2 Ajax - post input data

查看:304
本文介绍了Sweetalert2 Ajax - 发布输入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

用户点击按钮,转到某个页面,然后执行以下操作。

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>

我想要完成的事情,并且遇到困难的时候正在利用ajax发布来自输入字段textarea的数据。

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

我还想通过使用以下

'成功'

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

失败

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

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

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

<?php $ CustomerKey; ?>

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

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.

任何帮助都将不胜感激。

Any help would be greatly appreciated.

JSFiddle示例;

JSFiddle example;

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

推荐答案

您需要做的是在sweetalert的函数中进行ajax调用并传递客户键变量使用ajax的数据参数作为POST变量。

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.

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值,只需包含

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

$ CustomerKey = $ _POST [ 'CustomerKey'];

祝你好运

这篇关于Sweetalert2 Ajax - 发布输入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆