更新,解决:
毕竟,我发现我在更新ajax中调用了旧版本的代码.
‘boardControl.php’而不是’boardUpdate.php’这些是使编程变得有趣的错误.
我正在写一个浏览器gomoku游戏.我有ajax声明,允许玩家玩一块.
$(document).ready(function() {
$("td").live('click',function(){
var value = $(this).attr('id');
$.get('includes/boardControl.php',{play: value, bid: bid});
});
});
value =董事会的平方位置
bid =董事会ID
在为玩家识别创建用户登录之前,服务器端php有一个临时解决方案.它会在点击时旋转方块的片段状态,而不是知道要为其创建的播放器.
在创建登录内容后,我为玩家的ID设置了一个会话变量.我希望在ajax请求期间从php读取会话ID,并找出他们来自哪个播放器.
session_start();
…
$playerId = $_SESSION['char'];
$Query=("SELECT p1, p2 FROM board WHERE bid=$bid");
$Result=mysql_query($Query);
$p1 = mysql_result($Result,0,"p1");
$p2 = mysql_result($Result,0,"p2");
$newPiece = 0; //*default no player
if($playerId == $p1)
$newPiece = 1;
if($playerId == $p2)
$newPiece = 2;
出于某种原因,当我运行完整的Web应用程序时,即使在我删除代码以使它们循环之后,这些部分仍会循环.
此外,登录后如果我在浏览器中手动加载php页面,它会正确修改数据库(它只播放属于该播放器的片段)并输出正确的结果.
在我看来,当与Ajax一起使用时,会话不会被转移.然而谷歌搜索告诉我,会话确实与Ajax一起工作.
更新:我正在尝试提供更多信息.
>登录正常工作.我的
ID被识别,我打印出来
在董事会旁边确保
我正确地检索它.
> ajax请求会更新
板.传递的值是
用firebug确认并确认
安慰.但不是放置
只为他们的球员的棋子
属于它循环虽然片断
州(0,1,2).
>手动浏览时
boardUpdate.php并放入
从Ajax发送的相同值
在回声反应中看到的结果
表示相应的
片断每次播放
意.
>之后我的笔记本上的结果相同
新鲜的firefox.
>手动浏览
boardUpdate.php没有登录
在离开董事会之前
没有变化(没有用户时的预期)
在会话中找到).
>我已经仔细检查了那个
session_start()在php文件上
并仔细检查会话ID
变量.
希望这些额外的信息有所帮助,我已经没有想法告诉你什么了.我应该加载完整的代码吗?
更新2:
在检查了火灾中的Ajax响应后,我意识到“播放”请求没有得到结果,并且在下一次“更新”之前电路板不会更新.我还在调查这个,但我也会在这里发布给你们.
boardUpdate.php
值得注意的地方是:
刷新板(第6行)
放置件(第20行)
function boardUpdate($turnCount)(第63行)
session_start();
require '../../omok/dbConnect.php';
//*** Refresh Board ***
if(isset($_GET['update']))
{
$bid = $_GET['bid'];
$Query=("SELECT turn FROM board WHERE bid=$bid");
$Result=mysql_query($Query);
$turnCount=mysql_result($Result,0,"turn");
if($_GET['turnCount'] < $turnCount) //** Turn increased
{
boardUpdate($turnCount);
}
}
//*** Place Piece ***
if(isset($_GET['play'])) // turn order? player detect?
{
$squareID = $_GET['play'];
$bid = $_GET['bid'];
$Query=("SELECT turn, boardstate FROM board WHERE bid=$bid");
$Result=mysql_query($Query);
$turnCount=mysql_result($Result,0,"turn");
$boardState=mysql_result($Result,0,"boardstate");
$turnCount++;
$playerId = $_SESSION['char'];
$Query=("SELECT p1, p2 FROM board WHERE bid=$bid");
$Result=mysql_query($Query);
$p1 = mysql_result($Result,0,"p1");
$p2 = mysql_result($Result,0,"p2");
$newPiece = 0; //*default no player
if($playerId == $p1)
$newPiece = 1;
if($playerId == $p2)
$newPiece = 2;
// if($newPiece != 0)
// {
$oldPiece = getBoardSpot($squareID, $bid);
$oldLetter = $boardState{floor($squareID/3)};
$slot = $squareID%3;
//***function updateCode($old, $new, $current, $slot)***
$newLetter = updateCode($oldPiece, $newPiece, $oldLetter, $slot);
$newLetter = value2Letter($newLetter);
$newBoard = substr_replace($boardState, $newLetter, floor($squareID/3), 1);
//** Update Query for boardstate & turn
$Query=("UPDATE board SET boardState = '$newBoard', turn = '$turnCount' WHERE bid = '$bid'");
mysql_query($Query);
// }
boardUpdate($turnCount);
}
function boardUpdate($turnCount)
{
$json = '{"turnCount":"'.$turnCount.'",'; //** turnCount **
$bid = $_GET['bid'];
$Query=("SELECT boardstate FROM board WHERE bid='$bid'");
$Result=mysql_query($Query);
$Board=mysql_result($Result,0,"boardstate");
$json.= '"boardState":"'.$Board.'"'; //** boardState **
$json.= '}';
echo $json;
}
function letter2Value($input)
{
if(ord($input) >= 48 && ord($input) <= 57)
return ord($input) - 48;
else
return ord($input) - 87;
}
function value2Letter($input)
{
if($input >= 10)
return chr($input += 87);
else
return chr($input += 48);
}
//*** UPDATE CODE *** updates an letter with a new peice change and returns result letter.
//***** $old : peice value before update
//***** $new : peice value after update
//***** $current : letterValue of code before update.
//***** $slot : which of the 3 sqaures the change needs to take place in.
function updateCode($old, $new, $current, $slot)
{
if($slot == 0)
{// echo $current,"+((",$new,"-",$old,")*9)";
return letter2Value($current)+(($new-$old)*9);
}
else if($slot == 1)
{// echo $current,"+((",$new,"-",$old,")*3)";
return letter2Value($current)+(($new-$old)*3);
}
else //slot == 2
{// echo $current,"+((",$new,"-",$old,")";
return letter2Value($current)+($new-$old);
}
}//updateCode()
//**** GETBOARDSPOT *** Returns the peice value at defined location on the board.
//****** 0 is first sqaure increment +1 in reading order (0-254).
function getBoardSpot($squareID, $bid)
{
$Query=("SELECT boardstate FROM board WHERE bid='$bid'");
$Result=mysql_query($Query);
$Board=mysql_result($Result,0,"boardstate");
if($squareID %3 == 2) //**3rd spot**
{
if( letter2Value($Board{floor($squareID/3)} ) % 3 == 0)
return 0;
else if( letter2Value($Board{floor($squareID/3)} ) % 3 == 1)
return 1;
else
return 2;
}
else if($squareID %3 == 0) //**1st spot**
{
if(letter2Value($Board{floor($squareID/3)} ) <= 8)
return 0;
else if(letter2Value($Board{floor($squareID/3)} ) >= 18)
return 2;
else
return 1;
}
else //**2nd spot**
{
return floor(letter2Value($Board{floor($squareID/3)}))/3%3;
}
}//end getBoardSpot()
?>
请帮忙,如果需要,我很乐意提供更多信息.
在此先感谢=)