Sweetalert2 Ajax – 发布输入数据
我最近在我的项目上使用SweetAlert2,我想整理一个“添加注释”function。
用户单击按钮,将定向到页面,然后执行以下操作。
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" ) } })
我想要完成的,并且遇到困难的是利用ajax从输入字段“textarea”发布数据。
我还想通过使用以下内容来validation提交是成功还是失败
‘成功’
swal( "Sccess!", "Your note has been saved!", "success" )
“失败”
swal( "Internal Error", "Oops, your note was not saved." "error" )
我还需要将PHP对象传递给ajax(唯一的客户ID密钥),并允许ajax保存数据。
Sweet Alert没有提供关于如何使用ajax的大量文档,并且很难找到有关stackoverflow和在线搜索的问题的更多信息。
任何帮助将不胜感激。
JSFiddle示例;
https://jsfiddle.net/px0e3Lct/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" ) } })
并在您的php文件中获取customerKey值只包括
$CustomerKey = $_POST['CustomerKey '];
祝好运