3

I want to you the - SweetAlert for my Website. Now I need to configuration the SweetAlert with this code, so that on klick the "OK" button, it will send via POST formaction="/link/link" post="test=1"

swal({
    title: "Are you sure?",
    text: "You will not be able to recover this imaginary file!",
    type: " warning ",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    cancelButtonText: " No, cancel plx!",
    closeOnConfirm: false,
    closeOnCancel: false
}, function(isConfirm) {
    if (isConfirm) {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
    } else {
        swal("Cancelled", "Your imaginary file is safe: ) ", " error ");
    }
});

I want to ask you, how can I build it in.

Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58
pastuh
  • 41
  • 1
  • 1
  • 6

4 Answers4

5

Add you POST request trigger in the swal callback, like this:

swal({
    title: "Are you sure?",
    text: "You will not be able to recover this imaginary file!",
    type: " warning ",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    cancelButtonText: " No, cancel plx!",
    closeOnConfirm: false,
    closeOnCancel: false
}, function(isConfirm) {
    if (isConfirm) {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
        post('/link/link', {test: '1'});  // <<< This is what I added
    } else {
        swal("Cancelled", "Your imaginary file is safe: ) ", " error ");
    }
});

The definition of the function post() is found here

Community
  • 1
  • 1
Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58
2

I implemented Preconfirm method with Promise...

this.$swal({
            title: '¿Sure?',
            showCancelButton: true,
            confirmButtonText: 'Yes',
            cancelButtonText: 'No',
            showLoaderOnConfirm: true,
            preConfirm: function(result) {
                return new Promise(function(resolve, reject) {
                    if (result) {
                        axios.post('url', {data:data})
                        .then(function(response){
                            toastr.success('Succes');
                            resolve();
                        })
                        .catch(function(error){
                            toastr.error('Error ');
                            console.log(error);
                            reject();
                        })
                    }
                });
            },
            allowOutsideClick: () => !this.$swal.isLoading(),
        })
Efrén
  • 417
  • 5
  • 7
  • This worked for me. But not sure what supposed to do functions "resolve()" and "reject()", so I just ignored them. – eduardo a Feb 18 '19 at 18:46
  • 1
    Resolve method, it's return directly to "then" method if your use (in this case sweetalert); reject method return "false" and cancel operation – Efrén Feb 19 '19 at 19:09
1

For pure JS - this would post parameter post with value post123

var formTest = document.createElement('form');
formTest.setAttribute("method", "post");
formTest.setAttribute("action", "");

var post = document.createElement("input");
post.setAttribute("type", "hidden");
post.setAttribute("name", "post");
post.setAttribute("value", "post123");
formTest.appendChild(post);

document.body.appendChild(formTest);
formTest.submit();

If you want you can use short and nice AJAX calls which require jQuery

bigbobr
  • 355
  • 4
  • 17
0

try this code, just edit according to your need.

swal({
 title: "Are you sure?",
 text: "You will not be able to recover this imaginary file!",
 type: " warning ",
 showCancelButton: true,
 confirmButtonColor: "#DD6B55",
 confirmButtonText: "Yes, delete it!",
 cancelButtonText: " No, cancel plx!",
 closeOnConfirm: false,
 closeOnCancel: false },
  function() {
    $.ajax({
      type: "POST",
      data: {
        'data_id': id
      },
      url: 'http://localhost/project/method',
      success: function(data) {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
      },
      error: function(data) {
        swal("Cancelled", "Your imaginary file is safe: ) ", " error ");
      }
    });
  }
Fil
  • 8,225
  • 14
  • 59
  • 85