Jump to content

How to make the chat refresh without going to top and stay where its belong


shebbycs

Recommended Posts

<?php

$db = mysql_connect("localhost", "root") or die("Could not connect."); //username and password
mysql_select_db("cute") or die(mysql_error());

$getnummessages="SELECT COUNT(*) as messagecount from chatmessages";

$getnummessages2=mysql_query($getnummessages) or die("blah");

$getnummessages3= mysql_result($getnummessages2, 0);

if($getnummessages3>40)

{

   $startrow=$getnummessages3-20;

}

else

{

  $startrow=0;

}

$getmsg="SELECT name, message from chatmessages order by postime ASC limit $startrow,$getnummessages3";

$getmsg2=mysql_query($getmsg) or die(mysql_error());

function replacesmiley($msg)
{
         $msg = str_replace('', '<img src="smile.gif" alt="" />', $msg);
         $msg = str_replace('', '<img src="sad.gif" alt="" />', $msg);
         $msg = str_replace('', '<img src="tongue.gif" alt="" />', $msg);
         return $msg;
}

echo replacesmiley($text);

function filterBadWords($str)
{
    $result = mysql_query("SELECT badwords FROM clean") or die(mysql_error()); 
    $replacements = "*";
    
    while($row = mysql_fetch_assoc($result))
    {
          $str = eregi_replace($row['badwords'], str_repeat('*', strlen($row['badwords'])), $str);
    }  
    
    return $str;
}



while($getmsg3=mysql_fetch_array($getmsg2))

{

  $getmsg3['message'] = replacesmiley($getmsg3['message']);
  $getmsg3['message'] = filterBadWords($getmsg3['message']); //Smiley faces
  print "<font color='blue'><b>$getmsg3[name]:</b></font> $getmsg3[message]<br>";



}





?>

<script>

  setTimeout("window.location.replace('chatlog.php')",2000);



</script>

 

 

how to do when chat screen refresh its does not reload to the top of page and stay to where user are browse :)

Link to comment
Share on other sites

This page refreshes itself after a few seconds.  Don't do that.  Either let the user refresh it manually or use ajax to refresh all the data in the chart.  If you choose to use ajax, be prepared to LEARN AJAX, completely rewrite this page, and spend about 2 weeks on it at minimum.  There isn't just a "put the ajax right here" we can give you.  Ajax is a whole different way of thinking about content generation.

 

-Dan

Link to comment
Share on other sites

what is your question? cause it isn't clear o.0

 

 

this code " setTimeout("window.location.replace('chatlog.php')",2000);" will refresh but as its refresh the scroll going to be top as im want it to be where people scroll it even after refresh the place to scroll is same and not going top, this is my problem ;(

Link to comment
Share on other sites

<div id="AJAXstuffgoeshere"></div>

 

<script>

 

$.post('myphpfile.php', {var1:abc, var2:'a string'}, function(data){

$('#AJAXstuffgoeshere').html(data);

})

 

</script>

 

 

but still im cannot understand do you has exmaple where chatscreen is refresh using ajax :) thank you

Link to comment
Share on other sites

do you have a live site where we can see the code running?

 

So, my understanding of what you want is to have to have a chat log on the page and have it refreshed every 2 seconds. If you were handling more than 100 clients, I wouldn't encourage that. What you should actually do is to check if there is any updates on the chat log. If there is then you should update the chat log.

 

What imperium2335 had done was using jQuery's $.post() function.

 

Learn some jQuery here

 

From what I see, you don't speak English fluently, but hopes that site helps you.

 

If you want people to help you code your script, you can find them or post a new topic for help. Usually there are some charges from $0++

Link to comment
Share on other sites

do you have a live site where we can see the code running?

 

So, my understanding of what you want is to have to have a chat log on the page and have it refreshed every 2 seconds. If you were handling more than 100 clients, I wouldn't encourage that. What you should actually do is to check if there is any updates on the chat log. If there is then you should update the chat log.

 

What imperium2335 had done was using jQuery's $.post() function.

 

Learn some jQuery here

 

From what I see, you don't speak English fluently, but hopes that site helps you.

 

If you want people to help you code your script, you can find them or post a new topic for help. Usually there are some charges from $0++

 

 

better im give the screenshot

 

61799312245922815274.jpg

 

58096004068402662124.jpg

 

61799312245922815274.jpg

 

ok the first and second picture is chat screen where after  im using this code

 

<script>

  setTimeout("window.location.replace('chatlog.php')",2000);



</script>

 

it will refresh to the picture 3 im hope you can get my problem  question zul ;( as im want it refresh to where im scrolling and not after it refresh it will be going to top

Link to comment
Share on other sites

From the images, i see that you are using a div for the chat log. give the chatlog div an id for example

<div id="chatlog"><?php your stuffs here ?></div>

then add this script to the bottom of the script.

 

<script>
var objDiv = document.getElementById("chatlog");
objDiv.scrollTop = objDiv.scrollHeight;
</script>

Link to comment
Share on other sites

From the images, i see that you are using a div for the chat log. give the chatlog div an id for example

<div id="chatlog"><?php your stuffs here ?></div>

then add this script to the bottom of the script.

 

var objDiv = document.getElementById("chatlog");
objDiv.scrollTop = objDiv.scrollHeight;

 

 

ya i try to code

Link to comment
Share on other sites

try this. hope it works.

<?php

$db = mysql_connect("localhost", "root") or die("Could not connect."); //username and password
mysql_select_db("cute") or die(mysql_error());

$getnummessages="SELECT COUNT(*) as messagecount from chatmessages";

$getnummessages2=mysql_query($getnummessages) or die("blah");

$getnummessages3= mysql_result($getnummessages2, 0);

if($getnummessages3>40)

{

   $startrow=$getnummessages3-20;

}

else

{

  $startrow=0;

}

$getmsg="SELECT name, message from chatmessages order by postime ASC limit $startrow,$getnummessages3";

$getmsg2=mysql_query($getmsg) or die(mysql_error());

function replacesmiley($msg)
{
         $msg = str_replace('', '<img src="smile.gif" alt="" />', $msg);
         $msg = str_replace('', '<img src="sad.gif" alt="" />', $msg);
         $msg = str_replace('', '<img src="tongue.gif" alt="" />', $msg);
         return $msg;
}

echo replacesmiley($text);

function filterBadWords($str)
{
    $result = mysql_query("SELECT badwords FROM clean") or die(mysql_error()); 
    $replacements = "*";
    
    while($row = mysql_fetch_assoc($result))
    {
          $str = eregi_replace($row['badwords'], str_repeat('*', strlen($row['badwords'])), $str);
    }  
    
    return $str;
}

echo "<div id='chatlog'>";

while($getmsg3=mysql_fetch_array($getmsg2))

{

  $getmsg3['message'] = replacesmiley($getmsg3['message']);
  $getmsg3['message'] = filterBadWords($getmsg3['message']); //Smiley faces
  print "<font color='blue'><b>$getmsg3[name]:</b></font> $getmsg3[message]<br>";


}

echo "</div>";



?>

<script>

  setTimeout("window.location.replace('chatlog.php')",2000);

var objDiv = document.getElementById("chatlog");
objDiv.scrollTop = objDiv.scrollHeight;

</script>

Link to comment
Share on other sites


<div id="chatlog"><?php $db = mysql_connect("localhost", "root") or die("Could not connect."); //username and password
mysql_select_db("cute") or die(mysql_error());

$getnummessages="SELECT COUNT(*) as messagecount from chatmessages";

$getnummessages2=mysql_query($getnummessages) or die("blah");

$getnummessages3= mysql_result($getnummessages2, 0);

if($getnummessages3>40)

{

   $startrow=$getnummessages3-20;

}

else

{

  $startrow=0;

}

$getmsg="SELECT name, message from chatmessages order by postime ASC limit $startrow,$getnummessages3";

$getmsg2=mysql_query($getmsg) or die(mysql_error());

function replacesmiley($msg)
{
         $msg = str_replace('', '<img src="smile.gif" alt="" />', $msg);
         $msg = str_replace('', '<img src="sad.gif" alt="" />', $msg);
         $msg = str_replace('', '<img src="tongue.gif" alt="" />', $msg);
         return $msg;
}

echo replacesmiley($text);

function filterBadWords($str)
{
    $result = mysql_query("SELECT badwords FROM clean") or die(mysql_error()); 
    $replacements = "*";
    
    while($row = mysql_fetch_assoc($result))
    {
          $str = eregi_replace($row['badwords'], str_repeat('*', strlen($row['badwords'])), $str);
    }  
    
    return $str;
}



while($getmsg3=mysql_fetch_array($getmsg2))

{

  $getmsg3['message'] = replacesmiley($getmsg3['message']);
  $getmsg3['message'] = filterBadWords($getmsg3['message']); //Smiley faces
  print "<font color='blue'><b>$getmsg3[name]:</b></font> $getmsg3[message]<br>";



}






?></div>



<script>

var objDiv = document.getElementById("chatlog");
objDiv.scrollTop = objDiv.scrollHeight;



</script>

 

 

Which im left the part to refresh zul sir?

Link to comment
Share on other sites


<div id="chatlog"><?php $db = mysql_connect("localhost", "root") or die("Could not connect."); //username and password
mysql_select_db("cute") or die(mysql_error());

$getnummessages="SELECT COUNT(*) as messagecount from chatmessages";

$getnummessages2=mysql_query($getnummessages) or die("blah");

$getnummessages3= mysql_result($getnummessages2, 0);

if($getnummessages3>40)

{

   $startrow=$getnummessages3-20;

}

else

{

  $startrow=0;

}

$getmsg="SELECT name, message from chatmessages order by postime ASC limit $startrow,$getnummessages3";

$getmsg2=mysql_query($getmsg) or die(mysql_error());

function replacesmiley($msg)
{
         $msg = str_replace('', '<img src="smile.gif" alt="" />', $msg);
         $msg = str_replace('', '<img src="sad.gif" alt="" />', $msg);
         $msg = str_replace('', '<img src="tongue.gif" alt="" />', $msg);
         return $msg;
}

echo replacesmiley($text);

function filterBadWords($str)
{
    $result = mysql_query("SELECT badwords FROM clean") or die(mysql_error()); 
    $replacements = "*";
    
    while($row = mysql_fetch_assoc($result))
    {
          $str = eregi_replace($row['badwords'], str_repeat('*', strlen($row['badwords'])), $str);
    }  
    
    return $str;
}



while($getmsg3=mysql_fetch_array($getmsg2))

{

  $getmsg3['message'] = replacesmiley($getmsg3['message']);
  $getmsg3['message'] = filterBadWords($getmsg3['message']); //Smiley faces
  print "<font color='blue'><b>$getmsg3[name]:</b></font> $getmsg3[message]<br>";



}






?></div>



<script>
setTimeout("window.location.replace('chatlog.php')",2000);
var objDiv = document.getElementById("chatlog");
objDiv.scrollTop = objDiv.scrollHeight;



</script>

 

 

 

is this right?

Link to comment
Share on other sites

well, it works for me. you can try this.

 

<?php $db = mysql_connect("localhost", "root") or die("Could not connect."); //username and password
mysql_select_db("cute") or die(mysql_error());

$getnummessages="SELECT COUNT(*) as messagecount from chatmessages";

$getnummessages2=mysql_query($getnummessages) or die("blah");

$getnummessages3= mysql_result($getnummessages2, 0);

if($getnummessages3>40)

{

   $startrow=$getnummessages3-20;

}

else

{

  $startrow=0;

}

$getmsg="SELECT name, message from chatmessages order by postime ASC limit $startrow,$getnummessages3";

$getmsg2=mysql_query($getmsg) or die(mysql_error());

function replacesmiley($msg)
{
         $msg = str_replace('', '<img src="smile.gif" alt="" />', $msg);
         $msg = str_replace('', '<img src="sad.gif" alt="" />', $msg);
         $msg = str_replace('', '<img src="tongue.gif" alt="" />', $msg);
         return $msg;
}

echo replacesmiley($text);

function filterBadWords($str)
{
    $result = mysql_query("SELECT badwords FROM clean") or die(mysql_error()); 
    $replacements = "*";
    
    while($row = mysql_fetch_assoc($result))
    {
          $str = eregi_replace($row['badwords'], str_repeat('*', strlen($row['badwords'])), $str);
    }  
    
    return $str;
}



while($getmsg3=mysql_fetch_array($getmsg2))

{

  $getmsg3['message'] = replacesmiley($getmsg3['message']);
  $getmsg3['message'] = filterBadWords($getmsg3['message']); //Smiley faces
  print "<font color='blue'><b>$getmsg3[name]:</b></font> $getmsg3[message]<br>";



}






?>

<script>
setTimeout("window.location.replace('chatlog.html')",2000);
var objDiv = document.body;
objDiv.scrollTop = objDiv.scrollHeight;
</script>

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.