[英]Query database on beforeunload event
I was wondering if its possible to send a query to the database on the beforeunload event. 我想知道是否有可能在beforeunload事件中向数据库发送查询。
$(window).on('beforeunload',function() {
console.log('beforeunload called. run query');
});
If so, how would I do it? 如果是这样,我该怎么办? I need to run a query to insert dynamic values into the database. 我需要运行查询以将动态值插入数据库。
You could try this, but beware that the onbeforeunload
event is limited for certain browsers.. 您可以尝试这样做,但是请注意onbeforeunload
事件仅限于某些浏览器。
window.onbeforeunload = mySession;
function mySession(){
$.ajax({
url: "/../../myPHP.php",
async: false,
type: "GET"
});
return "WhatEverMessageYouFeelLike";
}
and your PHP executing query from AJAX handling.. 以及来自AJAX处理的PHP执行查询。
$delete = "DELETE FROM MyTable WHERE id=" .$_SESSION['mySessionVariable'];
// your query..
Use a jQuery AJAX call. 使用jQuery AJAX调用。 Assuming you're POSTing your data, try this: 假设您要发布数据,请尝试以下操作:
$.post(
'your/url',
{
your : "Post Vars"
},
function(data) {
// not sure if you'll need to do anything here actually.
},
'json' // or 'html', or whatever you want your return format to be
);
I thinks the best way to use post by jQuery is $.post() method.but u can also use $.ajax 我认为通过jQuery使用post的最好方法是$ .post()方法,但是你也可以使用$ .ajax
Sample way to use is 示例使用方式是
jQuery jQuery的
$.post( "ajax/test.html", function( data ) {
$( ".result" ).html( data );
});
jQuery Ajax jQuery Ajax
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.