PHP - How can I set the text cursor position

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

PHP - How can I set the text cursor position

Postby photobod » Tue Jan 02, 2007 12:36 pm

Hi All,

I've just about finished a web site for a client, but need to fine tune one particular script. This script runs a very simple "live chat" module.

The live chat page includes an iframe which calls a seperate page to fetch the chat content from mysql and drop it into a non-editable textarea in the iframe. This page reloads every few seconds so that it auto-updates.

A form with a textarea on the main page is used for input and reloads the whole page when submitted, adding the new text to the chat.

As the chat grows, the iframe gets scroll bars., but the content is always shown from the top of the block of messages.

How can I get the textarea to display the last few lines of the chat instead of the first few lines? I'm guesing that there should be a way of repositioning the text cursor, as one would in an editor, but I can't see a function anything like that.
photobod
 
Posts: 94
Joined: Thu Apr 21, 2005 7:18 pm
Location: London

RE: PHP - How can I set the text cursor position

Postby Diagmato » Wed Jan 03, 2007 1:27 am

Wouldnt it be better to display the chat content on a raw page inside a frame, rather than inside a text area?

As far as i know, the only way to re-position the text cursor is with javascript. Ive only dabbled with this a little, but looking at phpbb's subsilver theme on the posting template, it has javascript for working out where the cursor position is, in order to put bbcode tags around its location. Not exactly what you need, but should provide a clue.

...Even then it requires some ugly functions in order to detect the browser, and use a slightly different method, as the "simplest" way wont work across all browsers, it seems.
My Blog

Micro$oft gives you Windows, Linux gives you the full house
Diagmato
LXF regular
 
Posts: 272
Joined: Sat Apr 01, 2006 9:37 pm
Location: Cardiff

RE: PHP - How can I set the text cursor position

Postby M0PHP » Wed Jan 03, 2007 9:51 am

You could either only show the last 'X' messages, or simply put newest messages at the top.
Image Image Image
User avatar
M0PHP
LXF regular
 
Posts: 737
Joined: Wed Apr 06, 2005 7:40 am
Location: Bishop Auckland, County Durham, UK

RE: PHP - How can I set the text cursor position

Postby photobod » Thu Jan 04, 2007 11:25 am

Thanks for the suggestions, people.
Diagmato, you're quite correct about the textarea being redundant. Thinking about it, that was only there because I originally dabbled with the idea of embedding the textarea in the main page rather than in an iframe.
MOPHP, your second idea is a nice bit of lateral thinking! I don't think the first would help, as I can't be sure how much text will be in any one message anyway.
I had another thought myself. How about if I output an extra piece of text as a link at the bottom of the chat text, then have the page reload to that link? I guess I can disable it so it doesn't work as a link - perhaps also set the font colour to the same as the background so it doesn't show on screen?
photobod
 
Posts: 94
Joined: Thu Apr 21, 2005 7:18 pm
Location: London

RE: PHP - How can I set the text cursor position

Postby photobod » Thu Jan 04, 2007 12:15 pm

OK - tried my own suggestion by adding a named anchor at the bottom of the text called "end" and adding #end to the url in the refresh tag in the head, but that wouldn't work. The page wouldn't refresh itself at all.

I guess it's not possible to use a tag like <meta http-equiv=\"Refresh\" content=\"10; url=http://".$_SERVER["SERVER_ADDR"]."/mysite/showchat.php#end\">. Shame, as it's such a simple solution.

As a working fix, I've used MOPHP's suggestion and reversed the order of the messages so that the newest block is at the top of the list.

Thanks everyone for taking the time to reply.
photobod
 
Posts: 94
Joined: Thu Apr 21, 2005 7:18 pm
Location: London


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests