-4

I have a button where when user press, it shows yes or cancel if yes is pressed it redirects user to a url here is the code i am using

if (result.error == 2)
{
  if (confirm(result.message))
  {
    location.href = '/user.php?act=add_booking&id=' + result.goods_id + '&spec=' + result.product_spec; //
  }
}

Now i have installed Sweet alert 2 and i want to use it to confirm or cancel

John Snow
  • 11
  • 6
  • You must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – tgogos May 22 '17 at 13:43
  • 1
    Here's the [documentation](https://limonte.github.io/sweetalert2/) for SweetAlert2; you'll be most interested in the seventh example from the top. – Chris Forrence May 22 '17 at 13:45

3 Answers3

0

From your question i think now you have added Sweet Alert 2 Plugin.you want to change code for that

     swal({
  title: 'Are you sure?',
  text: "You will be redirected!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes'
},function(isConfirm){
  if (isConfirm) {
    window.location.href = "http://stackoverflow.com";
  }
  });
Gkrish
  • 1,811
  • 1
  • 14
  • 18
0

I have used this code and its working fine now

if (result.error == 2)
{
swal({
title: 'Are you sure?',
text: "Do you want to go to the Log In page?",
type: 'success',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, send me there!'
}, function(){
window.location.href = '/user.php?act=add_booking&id=' + result.goods_id + '&spec=' + result.product_spec;
});
}
John Snow
  • 11
  • 6
0

You just simply add .then((result) => { and create condition if it is confirmed or not then under that you can add some function or ajax or also you can redirect it using window.location.href = "your url"; }

I hope it will help! thanks!

$('#showsweetalert').click(function(){

      Swal.fire({
            title: 'Do you want to remove this picture?',
            showDenyButton: true,
            showCancelButton: true,
            confirmButtonText: 'Save',
            denyButtonText: `Don't save`,
        }).then((result) => {
            if (result.isConfirmed) {
                Swal.fire('Confirmed','You can add ajax function to redirect or to do some function to your back-end','success');
                // $.ajax({
                //     url: "Your-Url",
                //     method: 'POST',
                //     data: { 'data':'your-data'
                //         },
                //     success: function(data) {
                //         if (data.message == 'success') {
                //             you can use window.location to redirect to that specific url
                //             window.location.href = "your url";
                //         } else {
                //             swal.fire(data.message,'','error');
                //         }
                //     }
            }else{
              Swal.fire('Add some text','','error');
            }
        })

});
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.7.3/dist/sweetalert2.all.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<button id="showsweetalert"> Click me</button>
XRT-NoOne
  • 46
  • 8