Jump to content

EDITED: how do i refresh a certain forms textfield every 30 seconds ?


jasonc

Recommended Posts

I have started a chat script but face the problems in the page refreshing.

at present i use the meta-refresh method which is not a good idea, stops the user from entering text and clears the text they have typed in when refresh happens.

 

how do i get the page to refresh the textfields content if it changes, due to the other user they are chatting to entering text at they end and sending it.

 

either user sends a new message and this is added to the database, single mysql field containing al the text from both users.

 

 

user1:abc

user2:def

..

..

 

 

i wish for both users page to refresh if the database field has been updated.

 

how do i do this

Link to comment
Share on other sites

You create a javascript function which runs every x amount of seconds - this javascript function does an ajax call which retrieves all messages from the database for that chat, and using javascript to replace the current content.

 

You can spice it up with jquery and have content fade in etc.

 

EDIT:

 

Sorry, think I may have misunderstood. What you do is on your ajax call, you send the current content or the length of the current content to the php script. That then compares the content or length of with what is currently in the database - if there is more, or the content is different, load the new content.

 

But a better way is to have all messages stored in a single table with one message per row. Because no matter what, you are always going to make a mysql call.

 

Like this:

 

col user1

col user2

col msg

 

then when you check for an update you send the current amount of rows for between those people which you should make available on the page.

 

monitor(numrows){

//do ajax call

}

 

//php

 

$msgs = mysql_query("select * from messages where user1='person1' and user2='person2'");

 

if(mysql_num_rows($msgs) > $CURRENTNUMROWS){

// load new content and send back to ajax

}

 

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.