As a "learn while I build it" project, I'm attempting to construct a fairly simple tic-tac-toe game using mysql, php, jquery and ajax.
It lives here: http://www.dixieandtheninjas.net/test/tictactoe (don't be surprised that you can't even play tic tac toe yet, I'm learning)
My question is related specifically to doing long polling with ajax. I've "borrowed" from this thread: http://stackoverflow.com/questions/333664/simple-long-polling-example-code
But I have to get it to work correctly. It seems to just timeout without successfully completing.
I'm also curious how I can get it to .hide() a user's span when they logout (via window.unload). [edit: or do I need to hide them? will the ajax, when working correctly, only load people who are logged in, hence when someone logs out, they will simply no longer get displayed.. or will their span have to be specifically removed?]
You can check out my jquery at http://www.dixieandtheninjas.net/test/tictactoe/tictactoe.js
some of my PHP:
if (isset ($_POST['myname'])) {
$name = strtolower(mysql_real_escape_string(filter_input(INPUT_POST, "myname")));
//echo $name;
$_SESSION['name'] = $name;
//see if its in the db already
$result = mysql_query("select * from tictac_names where name='$name'", $db);
$rowCheck = mysql_num_rows($result);
echo $name;
if ($rowCheck > '0') {
// name is already in the db
//log them in
mysql_query("UPDATE tictac_names SET logged_on = '1' WHERE name ='$name'") or die(mysql_error());
} else if ($rowCheck < '1') {
// register them
$sql = <<< END
INSERT INTO tictac_names (name, logged_on, in_game)
VALUES ('$name', 1, 0)
END;
$result2 = mysql_query($sql) or die(mysql_error());
// then log them in
}
}
if (isset ($_POST['logout'])) {
if (filter_input(INPUT_POST,'logout') == '1') {
$name = $_SESSION['name'];
mysql_query("UPDATE tictac_names SET logged_on = '0' WHERE name ='$name'") or die(mysql_error());
echo "blarg";
}
}
if (isset ($_POST['longpoll'])) {
if (filter_input(INPUT_POST,'longpoll') == '1') {
$result = mysql_query("select name from tictac_names where logged_on='1'", $db);
$rowCheck = mysql_num_rows($result);
if ($rowCheck > '0') {
while ($row = mysql_fetch_assoc($result)){
foreach ($row as $val){
$spanid = 'sp_' . $val;
echo "<br><span id=\"$spanid\">$val</span></br>";
}
}
} // end rowcheck
}
}
Can anyone help me fix my polling? Can anyone give me some pointers on how I make it smart enough to both add names when they login, and remove names when they logout?