简体   繁体   English

Sweet Alert 2确认后再提交

[英]Sweet Alert 2 confirmation before submit

I'm trying to make a confirmation pop up before submitting the form. 我正在尝试在提交表单之前弹出确认消息。 But it's not working. 但这不起作用。 I'm using Sweet Alert 2. 我正在使用Sweet Alert 2。

document.querySelector('#order').addEventListener('submit', function(e) {
        var form = $(this).parents('form');
        e.preventDefault();
        swal({
            title: "Are you sure?",
            text: "Once a invoice is created, you will not be able to delete without the help of support",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: '#DD6B55',
            confirmButtonText: 'Yes, I am sure!',
            cancelButtonText: "No, cancel it!",
        }).then(function() {
            swal({
                title: 'Success!', 
                text: 'Invoice created! Go to the invoice tab to pay it.', 
                type: 'success'
            }, function() {
                form.submit();
            });
        },function(dismiss) {
            if(dismiss == 'cancel') {
                swal("Cancelled", "Invoice not created!", "error");
            }
        });
    });

That's my Javascript code and my PHP code looks something like this 那是我的Javascript代码,我的PHP代码看起来像这样

 <?php
if(isset($_POST['k'])
{
header('Location: https://google.com');
}
?>

My HTML code is 我的HTML代码是

<form method="POST">
<button type="submit" name="k"></button
</form>

It's not working why? 为什么不行呢?


Update 更新资料

$("#order").on('submit', function(e) {
        var form = $(this);
        e.preventDefault();
        swal({
            title: "Are you sure?",
            text: "Once a invoice is created, you will not be able to delete without the help of support",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: '#DD6B55',
            confirmButtonText: 'Yes, I am sure!',
            cancelButtonText: "No, cancel it!",
        }).then(function() {
            swal({
                title: 'Success!', 
                text: 'Invoice created! Go to the invoice tab to pay it.', 
                type: 'success'
            }, function() {
                $(this).trigger('submit');
            });
        },function(dismiss) {
            if(dismiss == 'cancel') {
                swal("Cancelled", "Invoice not created!", "error");
            }
        });
    });

Also I forgot to add that I parsed that code and on my actual code there is the ID on the form 此外,我忘了补充一点,我解析的代码和我的实际代码窗体上的ID

By not working I mean the data isn't being posted. 不工作是指未发布数据。 If it was working properly, it should be redirecting to google.com per the PHP code 如果运行正常,则应按照PHP代码将其重定向到google.com。

First, you don't have the selector #order in this HTML. 首先,此HTML中没有选择器#order

Second, you'll want to prevent the default behavior of the form element, so do this: 其次,您将要防止form元素的默认行为,因此请执行以下操作:

$("form").on('submit', function(e) {
    var form = $(this);
    e.preventDefault();
    //[...]

Then, where you want to finally submit, you can put this: 然后,在您要最终提交的位置,可以输入以下内容:

$(this).trigger('submit');

I am no coder but I found that placing the submit under .then(function() { was a working solution for me. The code I used is lp.jQuery("div.main-form form").submit(); but your form name will be different. I hope it helps in some way. 我不是编码人员,但是我发现将提交放在.then(function(){下对我来说是一个可行的解决方案。我使用的代码是lp.jQuery(“ div.main-form form”)。submit();但是您的表单名称会有所不同。我希望它会有所帮助。

$("#order").on('submit', function(e) {
    var form = $(this);
    e.preventDefault();
    swal({
        title: "Are you sure?",
        text: "Once a invoice is created, you will not be able to delete without the help of support",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: '#DD6B55',
        confirmButtonText: 'Yes, I am sure!',
        cancelButtonText: "No, cancel it!",
    }).then(function() {
      $(this).trigger('submit');
        swal({
            title: 'Success!', 
            text: 'Invoice created! Go to the invoice tab to pay it.', 
            type: 'success'
        }, function() {
            $(this).trigger('submit');
        });
    },function(dismiss) {
        if(dismiss == 'cancel') {
            swal("Cancelled", "Invoice not created!", "error");
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM