在JavaScript函数中调用PHP文件以更新MYSQL表?
我想将Java脚本老虎机游戏集成到我的脚本中 你可以在这里看到演示 git hub也在这里;你可以在这里看到所有的JS文件 我在用户表中创建了一个点栏,人们可以用这个点玩游戏 我认为slot.JS中的这个JS函数检查用户是赢了还是输了在JavaScript函数中调用PHP文件以更新MYSQL表?,php,javascript,mysql,function,Php,Javascript,Mysql,Function,我想将Java脚本老虎机游戏集成到我的脚本中 你可以在这里看到演示 git hub也在这里;你可以在这里看到所有的JS文件 我在用户表中创建了一个点栏,人们可以用这个点玩游戏 我认为slot.JS中的这个JS函数检查用户是赢了还是输了 function printResult() { var res; if(win[a.pos] === win[b.pos] && win[a.pos] === win[c.pos]) { r
function printResult() {
var res;
if(win[a.pos] === win[b.pos] && win[a.pos] === win[c.pos]) {
res = "You Win!";
} else {
res = "You Lose";
}
$('#result').html(res);
}
因此,如果用户赢了赌注,我想增加+100分
我为userid“1”编写了这段PHP代码
可以使用jQuery函数触发对PHP文件的异步请求
function printResult() {
var res;
if(win[a.pos] === win[b.pos] && win[a.pos] === win[c.pos]) {
res = "You Win!";
// Here's the line you need.
$.post('score.php', {userid: 1}, function(data) {
alert("Score saved.");
});
} else {
res = "You Lose";
}
$('#result').html(res);
}
这将把POST
数据发送到score.php
,或发送数据到的任何文件。然后,PHP文件可以通过检查$\u POST['userid']
的值来访问发送给它的userid
如中所述,$.post()
是jQuery的$.ajax()
函数的快捷方式,该函数经过简化,并预先设置了一些选项。$.post()
中的第三个参数是一个回调函数,,变量data
将包含执行时从score.php
中回显或打印的内容。因此,您可以使用alert(data)
来查看打印出来的score.php
内容。这对于故障排除和错误处理非常有用。您可以使用jQuery函数触发对PHP文件的异步请求
function printResult() {
var res;
if(win[a.pos] === win[b.pos] && win[a.pos] === win[c.pos]) {
res = "You Win!";
// Here's the line you need.
$.post('score.php', {userid: 1}, function(data) {
alert("Score saved.");
});
} else {
res = "You Lose";
}
$('#result').html(res);
}
这将把POST
数据发送到score.php
,或发送数据到的任何文件。然后,PHP文件可以通过检查$\u POST['userid']
的值来访问发送给它的userid
如中所述,$.post()
是jQuery的$.ajax()
函数的快捷方式,该函数经过简化,并预先设置了一些选项。$.post()
中的第三个参数是一个回调函数,,变量data
将包含执行时从score.php
中回显或打印的内容。因此,您可以使用alert(data)
来查看打印出来的score.php
内容。这对于故障排除和错误处理非常有用。您需要从javascript代码触发网络请求以执行php脚本服务器端。使用jQuery的函数是一种非常常见的方法,可以将各种浏览器差异抽象出来
function printResult() {
var res;
if(win[a.pos] === win[b.pos] && win[a.pos] === win[c.pos]) {
res = "You Win!";
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.ajax( "path/to/your.php" )
.done(function() { alert("success"); })
.fail(function() { alert("error"); })
.always(function() { alert("complete"); });
} else {
res = "You Lose";
}
$('#result').html(res);
}
您需要从javascript代码触发一个网络请求来执行php脚本服务器端。使用jQuery的函数是一种非常常见的方法,可以将各种浏览器差异抽象出来
function printResult() {
var res;
if(win[a.pos] === win[b.pos] && win[a.pos] === win[c.pos]) {
res = "You Win!";
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.ajax( "path/to/your.php" )
.done(function() { alert("success"); })
.fail(function() { alert("error"); })
.always(function() { alert("complete"); });
} else {
res = "You Lose";
}
$('#result').html(res);
}
试试这个
$(document).ready(function(){
setInterval(function() {
$.get("databaseUpdated.php");//or what ever your php file name is with corrct path
return false;
}, 1000);
});
希望这将帮助您在功能中使用它
function printResult() {
var res;
if(win[a.pos] === win[b.pos] && win[a.pos] === win[c.pos]) {
// if
setInterval(function() {
$.get("databaseUpdated.php");//or what ever your php file name is with corrct path
return false;
}, 1000);
} else {
res = "You Lose";
}
$('#result').html(res);
}
试试这个
$(document).ready(function(){
setInterval(function() {
$.get("databaseUpdated.php");//or what ever your php file name is with corrct path
return false;
}, 1000);
});
希望这将帮助您在功能中使用它
function printResult() {
var res;
if(win[a.pos] === win[b.pos] && win[a.pos] === win[c.pos]) {
// if
setInterval(function() {
$.get("databaseUpdated.php");//or what ever your php file name is with corrct path
return false;
}, 1000);
} else {
res = "You Lose";
}
$('#result').html(res);
}
不再支持mysql.*
函数,它们不再维护,将来也会维护。您应该使用或更新您的代码,以确保将来项目的功能。不再支持mysql.*
函数,它们不再维护,将来也将继续使用。您应该使用或更新您的代码,以确保将来项目的功能。我应该在何处添加此代码?我应该在何处添加此代码?我认为jQuery已经与此代码兼容;但当您尝试使用代码时,它会发出JS警报“错误”。你说得对!(虽然它是一个较旧的版本…更新它可能是一个好主意,因为它看起来不像一个庞大的依赖代码库)。谢谢你注意到这个@JacKy。嗯。。。您是否将“path/to/your.php”更改为.php文件所在的位置?(否则,“错误”警报正是我所期望的;-)您可能不需要更新jQuery就可以实现这一点。。。我不确定是什么原因导致了冻结,没有更详细地查看应用程序的其余部分。我正在我家的wamp服务器上运行这个程序。所以我把.php文件放在和游戏的.js和索引文件相同的文件夹中。像这样编辑ajax(“addpoint.php”)并像这样尝试“/addpoint.php”,但仍然会出错我认为jQuery已经被这段代码阻塞了;但当您尝试使用代码时,它会发出JS警报“错误”。你说得对!(虽然它是一个较旧的版本…更新它可能是一个好主意,因为它看起来不像一个庞大的依赖代码库)。谢谢你注意到这个@JacKy。嗯。。。您是否将“path/to/your.php”更改为.php文件所在的位置?(否则,“错误”警报正是我所期望的;-)您可能不需要更新jQuery就可以实现这一点。。。我不确定是什么原因导致了冻结,没有更详细地查看应用程序的其余部分。我正在我家的wamp服务器上运行这个程序。所以我把.php文件放在和游戏的.js和索引文件相同的文件夹中。然后像这样编辑ajax(“addpoint.php”),像这样尝试“/addpoint.php”,但仍然会出错