gpt4 book ai didi

javascript - 在 JS 条件下增加 php session

转载 作者:行者123 更新时间:2023-12-03 06:22:59 25 4
gpt4 key购买 nike

this.collision = function(p1, p2, p3, p4)
{
if(this.ballY - this.radius + this.dy <= p1.height && this.ballX + this.dx >= p1.leftPostx - p1.goalPostRadius && this.ballX <= p1.rightPostx + p1.goalPostRadius)
{
this.dy = -this.dy;
this.ballY = p1.height + this.radius;
count++;
score++;
<?php $_SESSION['score'] += 1; ?>
}
else if(this.ballY + this.radius + this.dy >= canvas.height - p2.height && this.ballX + this.dx >= p2.leftPostx - p2.goalPostRadius && this.ballX + this.dx <= p2.rightPostx + p2.goalPostRadius)
{
this.dy = -this.dy;
this.ballY = canvas.height - p2.height - this.radius;
count++;
score++;
<?php $_SESSION['score'] += 1; ?>
}
else if(this.ballX + this.radius + this.dx >= canvas.width - p3.height && this.ballY + this.dy >= p3.topPosty - p3.goalPostRadius && this.ballY + this.dy <= p3.bottomPosty + p3.goalPostRadius)
{
this.dx = -this.dx;
this.ballX = canvas.width - p3.height - this.radius;
count++;
score++;
<?php $_SESSION['score'] += 1; ?>
}
else if(this.ballX - this.radius + this.dx <= p4.height && this.ballY + this.dy >= p4.topPosty - p4.goalPostRadius && this.ballY + this.dy <= p4.bottomPosty + p4.goalPostRadius)
{
this.dx = -this.dx;
this.ballX = p4.height + this.radius;
count++;
score++;
<?php $_SESSION['score'] += 1; ?>
}
else if(this.ballY - this.radius + this.dy < 0 || this.ballY + this.radius + this.dy > canvas.height || this.ballX - this.radius + this.dx < 0 || this.ballX + this.radius + this.dx > canvas.width)
{
checkLives(p1, p2, p3, p4, ball1);
}
}

所以,标题说明了一切。在此代码中,我尝试使用 JavaScript 条件来确定何时递增 session 变量。我怎样才能做到这一点?预先感谢大家的帮助!

最佳答案

这样是不可能的。当你的脚本准备好并加载时,php代码就会执行,所以不依赖于你的js函数调用或js条件。永远记住 php 是服务器端脚本语言,而 JavaScript 是客户端脚本语言。

您可以尝试使用 ajax 或 jquery post。

这样尝试一下..

如果您没有添加 jQuery 库文件,那么您可以将其添加到标题中

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

现在你的代码是:

var myObj = new function(){
this.collision = function(p1, p2, p3, p4)
{
if(this.ballY - this.radius + this.dy <= p1.height && this.ballX + this.dx >= p1.leftPostx - p1.goalPostRadius && this.ballX <= p1.rightPostx + p1.goalPostRadius)
{
this.dy = -this.dy;
this.ballY = p1.height + this.radius;
count++;
score++;
}
else if(this.ballY + this.radius + this.dy >= canvas.height - p2.height && this.ballX + this.dx >= p2.leftPostx - p2.goalPostRadius && this.ballX + this.dx <= p2.rightPostx + p2.goalPostRadius)
{
this.dy = -this.dy;
this.ballY = canvas.height - p2.height - this.radius;
count++;
score++;
}
else if(this.ballX + this.radius + this.dx >= canvas.width - p3.height && this.ballY + this.dy >= p3.topPosty - p3.goalPostRadius && this.ballY + this.dy <= p3.bottomPosty + p3.goalPostRadius)
{
this.dx = -this.dx;
this.ballX = canvas.width - p3.height - this.radius;
count++;
score++;
}
else if(this.ballX - this.radius + this.dx <= p4.height && this.ballY + this.dy >= p4.topPosty - p4.goalPostRadius && this.ballY + this.dy <= p4.bottomPosty + p4.goalPostRadius)
{
this.dx = -this.dx;
this.ballX = p4.height + this.radius;
count++;
score++;

}
else if(this.ballY - this.radius + this.dy < 0 || this.ballY + this.radius + this.dy > canvas.height || this.ballX - this.radius + this.dx < 0 || this.ballX + this.radius + this.dx > canvas.width)
{
checkLives(p1, p2, p3, p4, ball1);
}

myObj.updateCore(score);
}
this.updateCore = function(addIncrement){
jQuery.ajax({
dataType: "json",
method:"POST",
url:PATH_TO_FILE+'/serverfile.php',
data:{score:addIncrement},
async:false,
beforeSend:function(){},
success:function(data){},
error:function(request, status, error) {
alert(request.responseText);
},
complete: function(){}
});
}
}

在serverfile.php中

<?php 

$_SESSION['score'] = $_POST['score'];

?>

关于javascript - 在 JS 条件下增加 php session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38782101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com