简体   繁体   中英

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="sweetalert2@10.js"></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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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