当前位置: 首页 > 工具软件 > SweetAlert2 > 使用案例 >

sweetalert2中ajax用法,Sweetalert2 Ajax - 发布输入数据(Sweetalert2 Ajax - post input data)

隆钊
2023-12-01

Sweetalert2 Ajax - 发布输入数据(Sweetalert2 Ajax - post input data)

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

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

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”发布数据。

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

'成功'

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示例;

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.

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"

)

}

})

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

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.

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

原文:https://stackoverflow.com/questions/46841570

更新时间:2019-12-12 17:46

最满意答案

您需要做的是在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 '];

祝你好运

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"

)

}

})

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

$CustomerKey = $_POST['CustomerKey '];

Good luck

相关问答

要使用finally()方法,您应该为您的页面包含Promise.prototype.finally的polyfill ,例如:

PS。 我刚刚将Promise.prototype.finally的链接添加到示例页面: https : Promise.prototype.finally To use finally

...

隐藏Ajax成功的当前模态。 尝试这个 $.ajax({

dataType: "json",

url: url,

type: "GET",

data: {some: data},

success: function (json) {

if (json.status === 'OK') {

hideSwal()

}

}});

function hideSwal(){

setTimeout(function(){

var button = document.getE

...

我建议你看一下从sweetalert到sweetalert2的迁移指南。 Sweetalert2不再使用回调,而是承诺。 您可以在此处找到所有迁移详细信息 编辑:这是你应该如何修改你的代码。 我正在接受你的第一个sweetalert电话。 这个电话: swal({

title: "Rider Saved and Classes Added!",

type: "success"

},

function (isConfirm) {

...

您应该将带有AJAX请求的回调传递给onOpen参数: swal.queue([{

title: 'Your public IP',

confirmButtonText: 'Show my public IP',

text:

'Your public IP will be received ' +

'via AJAX request',

onOpen: function () {

$.get('https://api.ipify.org?for

...

我不熟悉那个JS库,无论如何这是我认为应该工作的。 ui.R 首先,您必须将js脚本添加到您的页面(注意路径,它应该是CDN或/www/文件夹中的文件: tags$head(HTML(""))

server.R 然后在你回调你可以调用库函数: callback = JS('

table.on("dblclick.dt","tr", function()

...

您需要做的是在sweetalert的函数中进行ajax调用,并使用ajax的数据参数将客户键变量作为POST变量传递。 var CustomerKey = 1234;//your customer key value.

swal({

title: "Add Note",

input: "textarea",

showCancelButton: true,

confirmButtonColor: "#1FAB45",

confirmButtonText: "S

...

我为你尝试了一些东西,这是你想要的一样吗? function storyPublish(e)

{

$(".validated").jqBootstrapValidation(

{

preventSubmit: true,

submitError: function($form, event, errors) {

// Here I do nothing, but you could do something lik

...

你可以创建一个自定义的html文件,并有一个取消按钮,这将激发你自己的取消处理程序。 例如

confirm

cancel

$.get("custom.html", function (data) {

swal({

html: data,

...

而不是使用处理程序,您可以在窗体上使用处理程序,这样更好 给表单一个id也将你的submit按钮改为普通按钮因为我们将根据用户的选择手动提交(或不提交)

Username

...

 类似资料: