简体   繁体   English

在点击 sweetalert 弹出按钮后执行 PHP function

[英]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:这是我的 JavaScript function,它打开一个带有两个按钮(是和否)的甜蜜警报弹出窗口以确认注销或退出弹出窗口:

<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:

<?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?在弹出的甜蜜警报上单击“是”按钮后,有什么方法可以执行 PHP function 吗?

You can use HTTP Request.您可以使用 HTTP 请求。 For example you can use axios :例如,您可以使用axios

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

in php file you should just call this:在 php 文件中,您应该调用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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