Jump to content

help with ajax long polling


xjermx

Recommended Posts

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?

Link to comment
Share on other sites

  • 1 month later...

I'm posting in this thread again, as I'm still stuck at a dead end on this problem.

 

I'm not sure if there is simply an error in my code that makes the long-polling not work, or if I'm just trying the wrong solution here.

 

Any suggestions or help is most welcome!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.