-2

I'm using SweetAlert library from:

http://t4t5.github.io/sweetalert/

In detail, this alert box:

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) 
        {
            <?php SendMail($to); ?>
            swal("Deleted!", "Your imaginary file has been deleted.", "success");
        }
    else
        {
            swal("Cancelled", "Your imaginary file is safe :)", "error");
        }
});

As you can see, SendMail is allways called. How could I do to call SendMail just if isConfirm is true?

Thanks you so much,

John
  • 760
  • 5
  • 12
HugoRod
  • 33
  • 5
  • Javascript can't load PHP functions. But PHP can act like javascript files with the `header("Content-Type: text/javascript");` function – node_modules Mar 31 '16 at 14:31
  • That's not going to work. You'll have to use AJAX to send the email. – dokgu Mar 31 '16 at 14:48

1 Answers1

0

I think you are confusing how PHP & JavaScript works. PHP is executed server side long before that JavaScript gets executed in the client's browser. This is why SendMail is always called.

What you'll need to do is send an AJAX call back to the server when isConfirm is true so that you can then use your SendMail php function. AJAX calls allow you to send requests back to your server and run php code without reloading the page.

A library like jQuery makes this pretty easy.

Evan Taylor
  • 216
  • 3
  • 9