简体   繁体   English

当浏览器或浏览器选项卡关闭时从 jQuery 调用 PHP

[英]Call PHP from jQuery when browser, or browser tab closes

Below is jquery code that displays an alert before closing the tab or browser window:以下是在关闭选项卡或浏览器窗口之前显示警报的jquery代码:

$(window).on('beforeunload', function(){
    if(leave_window == false){
        debugger;
        var i =1;
        i++;
        alert('123'+i);
        return 'Are you sure you want to leave?';
    }  
});

I have a php function sendMail() that I would like to call from this code block but I am struggling to do so.我有一个php函数sendMail()我想从这个代码块调用它,但我很难这样做。 How can I call my php function before the browser closes?如何在浏览器关闭之前调用我的php函数?

$(window).on('beforeunload', function(){
    if(leave_window == false){
        debugger;
        var i =1;
        i++;


var data = {
    name: $("#form_name").val(),
    email: $("#form_email").val(),
    message: $("#msg_text").val()
};
$.ajax({
    type: "POST",
    url: "email.php",
    data: data,
    success: function(){

    }
});

 alert('123'+i);
        return 'Mail sent Successfully.. Are you sure you want to leave?';
    }  
});

in email.php在电子邮件.php

<?php
if($_POST){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['text'];

//send email
    mail("example@gmail.com", "51 Deep comment from" .$email, $message);
}
?>

Try this.. I think, it will give ua better solution for ur pblm试试这个..我想,它会为你的 pblm 提供更好的解决方案

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

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