Update, solution:
After all this, I found out that I was invoking an old version of my code in an ajax update. 'boardControl.php' instead of 'boardUpdate.php' These are the errors that make programming fun.
I am writing a gomoku browser . I have an ajax instruction that allows a player to play a piece.$(document).ready(function() {
$("td").live('click',function(){
var value = $(this).attr('id');
$.get('includes/boardControl.php',{play: value, bid: bid});
});
});
value = board square
bid = board ID
Before creating a user login to identify the player, the server-side php version had a temporary solution. It will rotate the piece state for the squares when pressed, instead of knowing which player creates them.
After creating the login materials, I set the session variable for the player ID. I was hoping to read the session id from php during an ajax request and find out which player they have.
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;
For some reason, when I run the full web application, the chunks are still looping, even after I removed the code to make them loop. In addition, after logging in. If I manually load the php page in the browser, it correctly modifies the database (where it plays only the parts belonging to this player) and displays the correct results.
It seems to me that the session is not portable when used with Ajax. However, Google search queries tell me that sessions work with Ajax.
Update: I am trying to provide additional information..
,
,
.
ajax
.
firebug's
.
,
(0,1,2).
boardUpdate.php
, Ajax
, -
,
.
firefox.
boardUpdate.php
( ,
).
I double checked that session_start () is in php files and double checked the session id variables.
Hope this additional information helps, I'm running out of ideas on what to tell you. Should I download the full code?
Update 2:After checking the Ajax response in the fire-bug, I realized that the "play" request does not get the result, and the board does not update until the next "update". I'm still learning this, but I'll post it here to you guys too.
boardUpdate.php
Known places: Refresh board (line6) Place Piece (line20) ($ turnCount) (line63)
<?php
session_start();
require '../../omok/dbConnect.php';
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)
{
boardUpdate($turnCount);
}
}
if(isset($_GET['play']))
{
$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;
if($playerId == $p1)
$newPiece = 1;
if($playerId == $p2)
$newPiece = 2;
$oldPiece = getBoardSpot($squareID, $bid);
$oldLetter = $boardState{floor($squareID/3)};
$slot = $squareID%3;
$newLetter = updateCode($oldPiece, $newPiece, $oldLetter, $slot);
$newLetter = value2Letter($newLetter);
$newBoard = substr_replace($boardState, $newLetter, floor($squareID/3), 1);
$Query=("UPDATE board SET boardState = '$newBoard', turn = '$turnCount' WHERE bid = '$bid'");
mysql_query($Query);
boardUpdate($turnCount);
}
function boardUpdate($turnCount)
{
$json = '{"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.'"';
$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);
}
function updateCode($old, $new, $current, $slot)
{
if($slot == 0)
{
return letter2Value($current)+(($new-$old)*9);
}
else if($slot == 1)
{
return letter2Value($current)+(($new-$old)*3);
}
else
{
return letter2Value($current)+($new-$old);
}
}
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)
{
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)
{
if(letter2Value($Board{floor($squareID/3)} ) <= 8)
return 0;
else if(letter2Value($Board{floor($squareID/3)} ) >= 18)
return 2;
else
return 1;
}
else
{
return floor(letter2Value($Board{floor($squareID/3)}))/3%3;
}
}
?>
Please help, I would be happy to provide additional information, if necessary. Thanks in advance =)