mani-hash
mani-hash

Reputation: 784

Execute a PHP function after clicking on sweetalert popup button

This is my JavaScript function that opens a sweet alert pop up with two buttons (yes and no) to confirm logout or exit the pop up:

<script src="[email protected]"></script>
<script>
function alert1() {
  Swal.fire({
    title: 'Logout',
    text: 'Do you want to log out?',
    showConfirmButton: 'true',
    confirmButtonText: 'Yes',
    showCancelButton: 'true',
    cancelButtonText: 'No',
    confirmButtonColor: '#00e600',
    cancelButtonColor: '#ff0000',
    imageUrl: 'Images/logo.png'
  }) 
}
</script>

This is my PHP function:

<?php

function logout() {
    setcookie("tem_username", "", time() - 3000, "/");
    setcookie("per_username", "", time() - 3000, "/"); 
}

?>

Is there any way I can execute the PHP function after clicking on the yes button on the sweet alert pop up?

Upvotes: 0

Views: 612

Answers (1)

Morani
Morani

Reputation: 498

You can use HTTP Request. For example you can use axios:

axios.get('/set_cookie.php').then(response => { console.log(response)}).catch(err => {console.log(error)})

in php file you should just call this:

function logout() {
 setcookie("tem_username", "", time() - 3000, "/");
 setcookie("per_username", "", time() - 3000, "/");
}
logout();

Upvotes: 1

Related Questions