hello guys
now i am working on a chat system and i am having a problem with the setTimeout can anyone help me this is all i have done
javascript -
$(document).ready(function()
-
{
-
updateMsg();
-
});
-
-
function updateMsg()
-
{
-
$.ajax({
-
url:"db.php",
-
type:"GET",
-
success:function(data){
-
addMessages();
-
}
-
});
-
setTimeout("updateMsg()",7000);
-
}
-
-
function addMessages()
-
{
-
$.ajax({
-
url:"db.php",
-
type:"GET",
-
data:"name="+$("#name").val()+"&to="+$("#user2").val()+"&cc="+$("#user").val()+"&msg="+$("#msg").val(),
-
success:function(data)
-
{
-
$("#t1").prepend(data);}
-
});
-
}
-
php -
foreach($_POST as $key => $value)
-
{
-
$key = mysql_real_escape_string($value, $dbconn);
-
}
-
-
-
-
$user_select = mysql_query("SELECT * FROM User WHERE User_name = '$name'")
-
or die(mysql_error());
-
$fetch_select = mysql_fetch_array($user_select);
-
$user_check = mysql_num_rows($user_select);
-
-
if ($user_check == 0)
-
{
-
mysql_query("INSERT INTO User (User_name) VALUES ('$name')") or die(mysql_error());
-
-
$new_user = mysql_query("SELECT * FROM User WHERE User_ID = LAST_INSERT_ID()");
-
-
$fetch_new_user = mysql_fetch_array($new_user);
-
-
mysql_query("INSERT INTO Messages(From_user,Msg_body,Date_Time)
-
VALUES ('$fetch_new_user[User_ID]','$msg',NOW())") or die(mysql_error());
-
}
-
-
else
-
{
-
mysql_query("INSERT INTO Messages(From_user,Msg_body,Date_Time)
-
VALUES ('$fetch_select[User_ID]','$msg',NOW())") or die(mysql_error());
-
}
-
-
$sql = mysql_query("SELECT Msg_body,Date_Time,User_name
-
FROM Messages,User
-
WHERE From_user = User_ID
-
AND Msg_ID = LAST_INSERT_ID()
-
ORDER BY Date_Time DESC") or die(mysql_error());
-
-
while($result = mysql_fetch_array($sql))
-
{
-
$mydata = '<tbody id="tbody1">
-
<tr class="highlight">
-
<td width="30" id="bullet" align="center">
-
<a href="#" class="nohighlight">•</a></td>
-
<td width="30px" align="center" id="replyImg"><input type="image" src="css/images/reply_arrow.png" onClick="reply()"></input></td>
-
<td width="70" align="Left" id="time">'.$result["Date_Time"].'</td>
-
<td width="200" align="Left" id="from">'.$result["User_name"].'</td>
-
<td width="200" align="Left" id="to">'.$result[""].'</td>
-
<td id="showMsg">'.$result["Msg_body"].'</td>
-
<td width="200" align="left" id="group">'.$result["Grp_abr"].'</td>
-
</tr>
-
</tbody>';
-
-
}
-
-
echo $mydata;
-
31 4158 gits 5,390
Expert Mod 4TB
i guess that you wanted to use setInterval() for continous execution ... or just call the timeout from the success-handler ...
kind regards
How? can you show me i have been working for 2 months on this and i am getting mad
gits 5,390
Expert Mod 4TB
:) just use: - window.setInterval(updateMsg, 7000);
-
or: - function updateMsg() {
-
$.ajax({
-
url: 'db.php',
-
type: 'GET',
-
success: function(data) {
-
addMessages();
-
window.setTimeout(updateMsg, 7000);
-
}
-
});
-
}
Hey i am so sorry but it didn't do the trick anyother ideas
i usually test it with 2 browser windows
gits 5,390
Expert Mod 4TB
post the code that you currently use ...
Hi
This is javascript -
$(document).ready(function()
-
{
-
updateMsg();
-
});
-
-
function updateMsg()
-
{
-
$.ajax({
-
url:"db.php",
-
type:"POST",
-
success:function(data){
-
alert("Hello");
-
}
-
});
-
setTimeout("updateMsg()",7000);
-
}
-
-
function addMessages()
-
{
-
$.ajax({
-
url:"db.php",
-
type:"POST",
-
data:"name="+$("#name").val()+"&to="+$("#user2").val()+"& cc="+$("#user").val()+"&msg="+$("#msg").val(),
-
success:function(data)
-
{
-
$("#t1").prepend(data);}
-
});
-
}
and this is php - $name = $_POST['name'];
-
$user2 = $_POST['to'];
-
$user = $_POST['cc'];
-
$msg = $_POST['msg'];
-
-
$user_select = mysql_query("SELECT * FROM User WHERE User_name = '$name'")
-
or die(mysql_error());
-
$fetch_select = mysql_fetch_array($user_select);
-
$user_check = mysql_num_rows($user_select);
-
-
if(isset($_POST['msg']) && $_POST['msg'] != '')
-
{
-
if ($user_check == 0)
-
{
-
mysql_query("INSERT INTO User (User_name) VALUES ('$name')") or die(mysql_error());
-
-
$new_user = mysql_query("SELECT * FROM User WHERE User_ID = LAST_INSERT_ID()");
-
-
$fetch_new_user = mysql_fetch_array($new_user);
-
-
mysql_query("INSERT INTO Messages(From_user,Msg_body,Date_Time)
-
VALUES ('$fetch_new_user[User_ID]','$msg',NOW())") or die(mysql_error());
-
}
-
-
else
-
{
-
mysql_query("INSERT INTO Messages(From_user,Msg_body,Date_Time)
-
VALUES ('$fetch_select[User_ID]','$msg',NOW())") or die(mysql_error());
-
}
-
}
-
-
$sql = mysql_query("SELECT Msg_body,Date_Time,User_name
-
FROM Messages,User
-
WHERE From_user = User_ID
-
AND Msg_ID = LAST_INSERT_ID()
-
ORDER BY Date_Time DESC") or die(mysql_error());
-
-
while($result = mysql_fetch_array($sql))
-
{
-
$mydata = '<tbody id="tbody1">
-
<tr class="highlight">
-
<td width="30" id="bullet" align="center">
-
<a href="#" class="nohighlight">•</a></td>
-
<td width="30px" align="center" id="replyImg"><input type="image" src="css/images/reply_arrow.png" onClick="reply()"></input></td>
-
<td width="70" align="Left" id="time">'.$result["Date_Time"].'</td>
-
<td width="200" align="Left" id="from">'.$result["User_name"].'</td>
-
<td width="200" align="Left" id="to">'.$result[""].'</td>
-
<td id="showMsg">'.$result["Msg_body"].'</td>
-
<td width="200" align="left" id="group">'.$result["Grp_abr"].'</td>
-
</tr>
-
</tbody>';
-
-
}
-
-
echo $mydata;
Use Firefox, then go to Tools -> Error Console to see your Javascript errors.
Dan
No errors at the error console
Even though i have firebug
gits 5,390
Expert Mod 4TB
you posted the same code as the first time .... with no adaptions i suggested? so try: - function updateMsg() {
-
$.ajax({
-
url: 'db.php',
-
type: 'POST',
-
success: function(data) {
-
alert('Hello');
-
window.setTimeout(updateMsg, 7000);
-
}
-
});
-
}
is the alert working? if so the same function should be called 7 seconds later on and the alert should occur again and so on ...
Hey Gits
Ya the function works but it doesn't retrieve anything from the database
may be i have a problem with my php code i don't know
gits 5,390
Expert Mod 4TB
the updateMsg() function just does an XMLHttpRequest to db.php ... without sending any data ... is that your intention?
No i want to retrieve the last data added to the database
gits 5,390
Expert Mod 4TB
i guess the latest messages for the specific user? then you would need to send the users name, id or whatever with the call ... as i said ... currently you post nothing to php with the current call from updateMsg() ...
Hi Gits
I totally changed the code what do you think now it is still not working
javascript - $(document).ready(function(){
-
timestamp = 0;
-
updateMsg();
-
$("form#chatform").submit(function(){
-
$.post("db.php",{
-
message: $("#msg").val(),
-
name: $("#name").val(),
-
action: "postmsg",
-
time: timestamp
-
}, function(data) {
-
$("#msg").empty();
-
addMessages(data);
-
});
-
return false;
-
});
-
});
-
function addMessages(data) {
-
alert(data);
-
//if($("status",data).text() == "2") return;
-
timestamp = $("time",data).text();
-
$("message",data).each(function(id) {
-
message = $("message",data).get(id);
-
$("#t1").prepend("<tr><td></td><td></td><td>"+$("name",message).text()+"</td><td></td><td>"+$("text",message).text()+"</td><td></td>");
-
});
-
}
-
/*<b>"+$("name",message).text()+
-
"</b>: "+$("text",message).text()+
-
"<br />*/
-
function updateMsg() {
-
$.post("db.php",{ time: timestamp }, function(data) {
-
addMessages(data);
-
});
-
setTimeout('updateMsg()', 4000);
-
}
-
php - foreach($_POST as $key => $value)
-
{
-
$$key = mysql_real_escape_string($value, $dbconn);
-
}
-
-
if(@$action == "postmsg")
-
{
-
mysql_query("INSERT INTO Messages (`Msg_body`,`Date_Time`)
-
VALUES ('$message',".time().")");
-
}
-
-
$messages = mysql_query("SELECT Msg_body
-
FROM Messages
-
ORDER BY id ASC");
-
if(mysql_num_rows($messages) == 0) $status_code = 2;
-
else $status_code = 1;
-
-
echo "<?xml version=\"1.0\"?>\n";
-
echo "<response>\n";
-
echo "\t<status>$status_code</status>\n";
-
echo "\t<time>".time()."</time>\n";
-
if($status_code == 1)
-
{
-
while($message = mysql_fetch_array($messages))
-
{
-
$message['Msg_body'] = htmlspecialchars(stripslashes($message['Msg_body']));
-
echo "\t<message>\n";
-
echo "\t\t<name>$message[From_user]</name>\n";
-
echo "\t\t<text>$message[Msg_body]</text>\n";
-
echo "\t</message>\n";
-
}
-
}
-
echo "</response>";
gits 5,390
Expert Mod 4TB
what JavaScript lib do you use? jQuery? ... what errors do occur?
jquery
No errors occur at all just not working
gits 5,390
Expert Mod 4TB
... if it's not working then somwhere has to be an error. so lets start to trace that down together. you start with: - $(document).ready(function() {
-
timestamp = 0;
-
-
updateMsg();
-
-
$("form#chatform").submit(function() {
-
$.post("db.php", {
-
message: $("#msg").val(),
-
name: $("#name").val(),
-
action: "postmsg",
-
time: timestamp
-
}, function(data) {
-
$("#msg").empty();
-
addMessages(data);
-
});
-
return false;
-
});
-
});
-
the first request is done by updateMsg() and the second is instantly done after this by submitting some form-data. so in both success-handlers you might trace the response ... what do you get back there?
Hey gits
I changed again to return an xml response now i want to know where to place the setTimeout - function addMessages()
-
{
-
//alert($("#msg").val());
-
$.ajax({
-
url:"db.php",
-
type:"post",
-
data:"message="+$("#msg").val(),
-
datatype:"xml",
-
success:function(data)
-
{ $("#t1").prepend('<tr><td>bullet</td><td>reply</td><td>'+$("time",data).text()+'</td><td>De</td><td>A</td><td>'+$("message",data).text()+'</td><td>Gr</td></tr>');
-
}
-
});
-
}
-
-
function updateMsg()
-
{
-
$.ajax({
-
url:"db.php",
-
type:"post",
-
datatype:"xml",
-
success:function(data)
-
{
-
addMessages();
-
}
-
});
-
}
Thanks
gits 5,390
Expert Mod 4TB
inside a success-handler and just call the method that should be called again.
Hi Gits
HELP ME
I am going crazy
take a look at the code and tell me what do you think
Can you test it please because if it doesnt work i will kill myself - $(document).ready(function()
-
{
-
updateMessages();
-
-
})
-
function addMessages()
-
{
-
$.ajax({
-
url:"db.php",
-
type:"post",
-
data:"message="+$("#msg").val()+"&action="+"post",
-
success:function()
-
{
-
updateMessages();
-
}
-
-
});
-
}
-
function updateMessages()
-
{
-
$.ajax({
-
url:"db.php",
-
type:"post",
-
data:"action="+"update",
-
datatype:"xml",
-
success:function(data)
-
{ $("#t1").prepend('<tr><td>bullet</td><td>reply</td><td>'+$("time",data).text()+'</td><td>De</td><td>A</td><td>'+$("message",data).text()+'</td><td>Gr</td></tr>');
-
-
}
-
-
});
-
setTimeout("updateMessages",7000);
-
}
gits 5,390
Expert Mod 4TB
wait a moment. let's first check what you have. is the code from post #19 working without a timeout ... so i mean ... do you get the result on the page the first time when it runs?
It runs onone page not the other
I want when i send the message it appears on both pages
gits 5,390
Expert Mod 4TB
ok i'm simply confused now. lets have a look at your latest code (i guess you use the code from post #21), since that is the latest thing that is to follow at the moment. when the doc is ready you call updateMessages() which itself should call updateMessages() after 7secs again but you have a syntax error here: - setTimeout("updateMessages", 7000);
must be: - setTimeout(updateMessages, 7000);
the better way would be to place it in the success-handler but the current calls should work then. your function addMessages() is useless now since it is never called?
and what do you mean with 2 pages? this is the first time you mention that it isn't just one?
Hey Gits
I want to achieve chatting between 2 clients so this is why i am using setTimeout
I want when i write a message on one page it appears on the 2 pages
If you have other questions this is my email [edit]removed[edit]
bacause i want to solve this problem
gits 5,390
Expert Mod 4TB
please don't post email-addresses in the technical forums, or do you want to get spammed? :) ... and at least it is against the posting rules.
to the problem: i think i don't get it quite well, let me summarize:
- you have written a webclient for a webchat-application
- you open it and log in as User A
- you are able to post msg and see the messages at this page?
-> that is the code that i saw until now ...
- now you open another page and open the client again
- you login with another user B
- here is the problem? what is not working? i don't understand what the timeout should have to do with that ... since both of the windows should use the same code?
Hi Gits
sorry i must have forgotten the rules
yes this is what i do
yes when i login with another user there is lag in time between messages
gits 5,390
Expert Mod 4TB
so the problem is the lag in time? ...
Hey Gits the problem is that the message don't appear on same pages at the same time
I want it to appear as if both clients are chatting when i send from one page last messages appear on both pages
This is the problem
gits 5,390
Expert Mod 4TB
this will not work ... you will always have to poll the messages from the client ... and in case you don't want to poll every second then you will have that delay ... that is the design of web-technology ...
So what should i do any ideas
gits 5,390
Expert Mod 4TB
you have to live with that issue ... what is the problem? max time between the appearing of the message after send is 7seconds ... to reduce it you might poll every second ... ? isn't that enough? with a webpage-based chat-client you don't have stateful connections and you cannot push the messages ...
Sign in to post your reply or Sign up for a free account.
Similar topics
by: domeceo |
last post by:
can anyone tell me why I cannot pass values in a setTimeout function
whenever I use this function it says "menu is undefined" after th
alert.
function imgOff(menu, num) {
if (document.images) {...
|
by: A |
last post by:
Hi all!
I am currently working on a project where I need to create a web-based chat
application that will be used for auctions.
If possible, could I get some advice on how to create chat...
|
by: John Bokma |
last post by:
Hi,
I am working on a simple chat program and want to use AJAX. In order to
get the new lines added to the chat I need to call a server-side script,
say every x seconds.
A second way to get...
|
by: jason.m.ho |
last post by:
Hello!
I am trying to build an ajax chat system. Currently I have it so that
when you chat, you upload your message to the database, and each client
is polling that database to see if he/she has...
|
by: nikki_herring |
last post by:
I am using setTimeout( ) to continuously call a function that randomly
rotates/displays 2 images on a page. The part I need help with is the
second image should rotate 3 seconds after the first...
|
by: The1corrupted |
last post by:
Alright, I've set up a chat system that only works half way... I need suggestions on how to make it work better...
I'm trying to figure out a way to make $_SESSION viewable to all players by...
|
by: quill |
last post by:
Hi
I am making a chatroom script and it appears that the problem seems to be that my setTimeout's are conflicting.
The logic is as follows:
Run a login check every x seconds
Run a trigger...
|
by: oll3i |
last post by:
how do i run this example ? with openjms
package com.ociweb.jms;
import java.io.*;
import javax.jms.*;
import javax.naming.*;
|
by: oll3i |
last post by:
when i run it from bat (@start "CHAT to topic" run Chat)without durable subscriber the window for chat opens but when i added the durable subscriber to the code it stopped opening?
import...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |