PHP & Ajax
Posted: Mon Nov 01, 2010 2:00 pm
Alright... so what I'm trying to do is create a client that connects to a chat server. The frontend is all done in AJAX (jquery to be specific) and it's powered by PHP.
My only problem right now that seems to be giving me trouble is relaying the data the PHP recieves back to AJAX so it than can append the data to my chat box.
Here is what I have for my request...
This opens botmain.php which creates a class that initiates the connection sequence. The responses (such as Connecting.. Connected, etc..) in the class calls a function called "echo_flush" which just echos and just flushes.
This is pretty self explanatory. Anyways, what's happening is when ajax is requesting the page, every new echo is stacked. Meaning, it will do this in the chat window.
"Connecting..."
"Connecting...
Connected!"
"Connecting...
Connected!
Sending login information..."
An so on. Is there a better way to communicate to AJAX with PHP without using the XMLHttpRequest object?
Thanks in advanced!
My only problem right now that seems to be giving me trouble is relaying the data the PHP recieves back to AJAX so it than can append the data to my chat box.
Here is what I have for my request...
Code: Select all
function connect()
{
var page = getXHRObject();
page.onreadystatechange = function()
{
if (page.readyState == 3 && page.status == 200)
parse(page.responseText);
}
page.open("GET", "botmain.php", true);
page.send();
}
Code: Select all
function addChat(msg_type, icon, text)
{
var msgtype_pln;
var icon_str;
if (msg_type === undefined) msg_type = 0;
if (icon === undefined) icon = 0;
if (msg_type == 0)
msgtype_pln = 'ui-state-highlight';
else
msgtype_pln = 'ui-state-error';
switch (icon)
{
case 0:
icon_str = '';
break;
case 1:
icon_str = 'class="ui-icon ui-icon-info" ';
break;
case 2:
icon_str = 'class="ui-icon ui-icon-person" ';
break;
case 3:
icon_str = 'class="ui-icon ui-icon-alert" ';
break;
case 4:
icon_str = 'class="ui-icon ui-icon-notice" ';
break;
}
$('.bot_chat').append('<div class="' + msgtype_pln + ' ui-corner-all" style="margin-top: 1px; padding: 0 .7em;"><p><span ' + icon_str + 'style="float: left; margin-right: .3em;"></span>' + text + '</p> </div>');
$('.bot_chat').scrollTop($('.bot_chat')[0].scrollHeight);
}
"Connecting..."
"Connecting...
Connected!"
"Connecting...
Connected!
Sending login information..."
An so on. Is there a better way to communicate to AJAX with PHP without using the XMLHttpRequest object?
Thanks in advanced!