Javascript 如何在脚本中使用sql查询
我有一个只需点击一个按钮就可以执行的脚本。它工作得很好。有两点我想运行deletequery来删除db条目 1) 我在脚本中有一个代码,用于检查用户是否重新加载或按下了“后退”按钮。代码是:Javascript 如何在脚本中使用sql查询,javascript,jquery,mysql,ajax,mysqli,Javascript,Jquery,Mysql,Ajax,Mysqli,我有一个只需点击一个按钮就可以执行的脚本。它工作得很好。有两点我想运行deletequery来删除db条目 1) 我在脚本中有一个代码,用于检查用户是否重新加载或按下了“后退”按钮。代码是: window.onbeforeunload = function(e) { return 'You will loose a chance of negotiation for today. Are you sure you want to exit? '; /***Run
window.onbeforeunload = function(e)
{
return 'You will loose a chance of negotiation for today. Are you sure you want to exit? ';
/***Run delete query for deleting db entry***/
};
If the user reloads or goes to previous page even after the warning i would like to run delete query
2) 我的脚本中有以下部分,希望在最后运行删除查询
var ticker = function() {
counter--;
var t = (counter / 60) | 0; // it is round off
digits.eq(0).text(t);
t = ((counter % 60) / 10) | 0;
digits.eq(2).text(t);
t = (counter % 60) % 10;
digits.eq(3).text(t);
if (!counter) {
clearInterval(timer);
alert('Time out !');
resetView();
}
};
问题是,我不确定如何在不让用户知道的情况下在脚本中编写sql查询(查询将在后端运行)。删除查询应该是这样的
$sql="DELETE FROM product where id='".$id."'";
if(!mysqli_query($con,$sql))
{
"Error deleting record:" . mysqli_error($con);
}
有人能告诉我如何组合代码吗
我尝试使用的ajax代码
window.onbeforeunload = function(e)
{
$.ajax({
type: 'post',
url: 'test2.php',
dataType: 'json',
data: {
txt: txtbox,
hidden: hiddenTxt
},
cache: false,
console.log(returndata);
},
error: function() {
console.error('Failed to process ajax !');
}
});
};
为此,您应该使用AJAX。看一看,应该可以开始了。因为它在后端,所以在php函数中(例如:
deleteRecord
)可以有一个带有相关id
的ajax请求
),确保用户已登录(cookie存在,等等)然后运行查询。@Ofir Baruch当该进程运行时,用户已登录,我使用了ajax,但它不起作用。请考虑与我们共享您尝试过但不起作用的ajax代码。另外,请注意,不起作用
并不能真正帮助我们找到问题所在-到底什么不起作用?有什么警告吗?有输出吗?@ofirbarch我编辑了我的代码,控制台中没有任何错误,但它在网络选项卡->查找test2.php
行->打开预览选项卡下停止了我的整个脚本。你看到了什么?我为ajax尝试了以下代码:$.ajax({type:'post',url:'test2.php',dataType:'json',data:{txt:txtbox,hiddenTxt},cache:false,console.log(returndata);},error:function(){console.error('Failed to process ajax!');});但它不起作用