Below is code I am using and it's not working. As you can see in code,
I have coded it to display alert if there is issue with ajax, and
every time I run script I got error.
var c = new Array();
function ajaxRequest(loc,callback) {
new Ajax.Request(loc, {
method: 'post',
onSuccess: callback
});
}
function chat(type,e) {
if(type == 'submit') {
if(!ajaxRequest('/chat.php?a=chat_post&m='+encodeURIComponent
(e.value),'')) {
alert('Cannot post your message!');
}
e.value = '';
e.focus();
} else if(type == 'receive') {
if(!ajaxRequest('/chat.php?a=chat_list','rec_content')) {
alert('Cannot receive messages list!');
clearInterval(cs);
}
}
}
function rec_content(o) {
if(o.list) {
if(c[1] != o.list) {
c[1] = o.list;
$('#chatwindow').update(o.list);
}
}
}
Event.observe(window,'load',function() {
cs = setInterval("chat('receive','#chatwindow')",1000);
});</code>
and this is chat.php file
<?php
$a = htmlspecialchars($_GET['a']);
if(!mysql_connect('localhost','root','pass') || !mysql_select_db
('db')) {
exit();
}
if($a == 'chat_list') {
$query = mysql_query("SELECT * FROM messages");
$o = '<ul>';
while($row = mysql_fetch_assoc($query)) {
extract($row);
if(($time - microtime(true)) < 86400) {
$o .= '<li><b>'.$user.':</b> '.
$message.'</li>';
}
}
$o .= '</ul>';
echo '{\'list\':'.json_encode($o).'}';
} elseif($a == 'chat_post') {
$m = htmlspecialchars($_GET['m']);
$u = 'Guest';
$n = microtime(true);
$query = mysql_query("INSERT INTO messages(user,message,time)
VALUES ('$u','$m','$n')");
}
?>
It always outputs me an error, because I have set that it output an
alert if ajaxRequest is not created.
--
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en.