Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using AJAX + PHP to display logged users - Please help!

Status
Not open for further replies.

casabella

Programmer
Aug 30, 2007
59
0
0
US
I have a division within my main page

Code:
<div id="chatUserList"></div>

I have a JS
Code:
/**
 * @author JPA
 */
var timer = null;
var chatUser;
var xo = null;
var url = 'listChatUser.php';

function createObject(){
	var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        xo = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        xo = new XMLHttpRequest();
    }
}
function updateChatUser(){
    if (!xo) {createObject();}
    xo.onreadystatechange = function () {
            if (xo.readyState == 4) {
                if (xo.status == 200) {
                    if(xo.responseText == ''){
                        //do nothing
                    } else {
						//alert(xo.responseText);
                        displayUpdatedUsers(xo.responseText);
                    }
                }
            }
        };
    try {
        xo.open("POST",url, true);
        } catch (e) {    
        console.log('problem in open command') ;
    }
    try {
        xo.setRequestHeader("Content-type", "application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]
        xo.send('action=listusers');
    } catch (e) {
        console.log('problem retrieving new chats');
    }
}
function displayUpdatedUsers(txt){
	chatUser.innerHTML =  txt;
}

window.onload = function(){
	chatUser = document.getElementById('chatUserList');
	timer = setInterval(updateChatUser, 5000);
};

My PHP script looks as follows
Code:
<?

$sql = "SELECT * FROM chatuser WHERE userName != '' ORDER BY userLoginTimeStamp DESC";
$dbserver = mysql_connect("localhost","user","password");
mysql_select_db("dbname", $dbserver);
$data = mysql_query($sql);

$return = "<select>";
while ($row = mysql_fetch_assoc($data))
{
$return .= "<option>" . $row['userLogInTimeStamp'] . " " . $row['userName'] . "</option>";
}
$return .= "</select>";

echo $return ;

?>

If I run the PHP script by itself, I do get the drop down list. I have tried not to ask for help (I have to learn this) but after spending all afternoon and this evening looking at it, I think I will never find the problem; no matter how simple it is.

Regards,


Jose
 
Oh, I guess it is worth mentioning that my main page has

Code:
if ($_REQUEST['Target'] = "chat" || $_REQUEST['Target'] == "Chat" || $_REQUEST['Target'] == "CHAT")
{
echo '<script src="chatroom/listChatUser.js"></script>';
}

to make sure script is loaded only when needed. I know the script is loaded because if I set the command
Code:
alert(xo.responseText);

the alert window pops up but shows blank ... nothing is returned or received from listChatUser.php.


Here is the link of the chat room I am working on


The list should show on the right hand side bar.

Regards,


Jose
 
try calling the page with the php script statically and see what comes out. footprint your code with debug commands too (print_r the value of the arrays etc)
 
jpadie,

I did what you suggest and the user list was shown in every page but the chat room. I continued troubleshooting and found that I was calling/linking the JS a second time (within the chat room page), I removed it and the list does not go away in the chat room.

A new problem emerged, this is what the list shows
Code:
{"timestamp":1189345056,"chatText":"\r\n\tJose Lerebours<\/td>\r\n\t18:08:01<\/td>\r\n\tHello World!<\/td>\r\n<\/tr>\r\n\tjosel<\/td>\r\n\t09:27:50<\/td>\r\n\ttesting chat<\/td>\r\n<\/tr>"}

Yep, it is showing the chat text not the user list. Worst yet, it shows it with HTML stripped so you can see everything - looks like caca (or is it kaka?) ...

I figure that the scripts are colliding since they are using same variable xo. I then edited the JS (posted above) that calls the user list and changed every instance of xo for xu. As I did this, the code breaks and I get an error saying
Code:
xu has no properties

Am I limited to using ONE JS script to handle all XMLHTTPREQUESTs and ONE PHP script to handle calls? Is this what we refer to as memory leak?

This really does not make much sense. But then again, I do not know the IN and OUT of JS so I cannot say much. I would, however, expect that each script loads and runs independently of one another.

Any suggestion will be appreciated.

Thanks,


Jose

 
Jose

no - it's normal to reuse the xmlhttp object, although if you have concerns about this, the javascript forum is a better place to ask than here.

by the way, this won't work
Code:
$_REQUEST['Target'] = "chat"

because the result will ALWAYS be true. the = sign is an assignment operator and not a comparison. use == or ===

if you're still using my code, you need to eval() the result to turn the JSON string into a usable object.

in other respects, I am not following you, i'm afraid. could you perhaps post the code you are using up here or if it's too much, zip it up and sent it to me via my website document submission mechanism:
 
jpadie,

I finally got the chat room to work. I tell you, I have learned a lot and I truly appreciate your help. I cannot remember how many times I broke the code and fixed it, only to break it again. I guess this is what one has to go through to learn the IN and OUT of programming.

I have made the following changes to the original code:
1) Moved entry fields to bottom and displaying content on top
2) Added a who list (logged in chatters)
3) Keep track of chatter last post
4) Content is shown within an iframe (will change to div)
5) Since I do not require chatters to log in, they are automatically logged the moment they blur out of the name field

TO DO:
1) Remove chatters that have not posted for past X minutes
2) Smilies?
3) Commands?
4) Private messaging

I do not think I could have gotten this far had you not helped me. I am sure that like me, there are many others that could benefit from this little project.

Not exactly done but pleased with my slow and steady progress.

Regards,


Jose Lerebours
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top