Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I am in the process of constructing a chat room via my web site. I've created a cgi script which in turn generates an HTML page composed of several frames, one being the main window for the chat(text) to be displayed, and a second smaller frame, where I have my textbox and 'submit' button. Unfortunately, I'm currently at work, so I don't have access to my code for examples.

I have completed the logging in process, which brings the user to the pages of frames. I can enter text for the chat bar at the bottom, and submit it, but I would like to know how I can submit the information silently to the main chat window and refresh the textbox, and remove the text from it after submission. I'm sure this is a simple task, but I'm stuck, does anyone have a suggestion?

Thanks again!

Replies are listed 'Best First'.
Re: CGI & HTML Frames
by andreychek (Parson) on Aug 15, 2002 at 18:34 UTC
    Another method you might be able to use is with the OpenThought Web Application Environment. OpenThought allows you to communicate with the server, and update the browser window, without actually reloading the webpage.

    This would allow you to build your chat room, without needing seperate frames for each component. So, lets say we have a textarea where the chat message goes called "chatwindow", and a textarea where the user types in their message called "newtext", with an associated submit button right next to it. The way it would work then is the user would type in their new text into the "newtext" textarea, and hit submit. The page doesn't refresh -- instead, the data the user typed in is sent to the server, where you can do any amount of processing on it, and then respond back to the browser. In this case, your response might be to update the "chatwindow" textarea with the latest text, while clearing out the "newtext" textarea.

    The code to make that work would look something like:
    The HTML: <form name='chat'> <textarea name='chatwindow'></textarea> <br/><br/> <textarea name='newtext'></textarea> <input type='button' name='sendtext' value='Send!' onClick="parent.SendParameters('chatserver.pl','newtext', 'run_mode +=sendchatmessage')"> <br/> </form> And on the server side, we have a subroutine within a Perl script whic +h looks like: sub sendchatmessage { my $self = shift; # Retrieve the text message sent to us from the browser my $chatmsg = $self->OP->param->get_incoming('fields')->{'newtext' +}; # Perhaps we want to log all messages log_message( $chatmsg ); # Get the last 10 messages which have been sent to # the chat server (including the one we just logged) my $messages = get_last_ten(); # Prepare to send data back to the browser. # Each hash key represents a field in our HTML my $field_data = { chatwindow => $messages, # Put the last 10 messages in the cha +t window newtext => "", # Blank out the newtext field }; # Send the data back to the browser. # This puts the appropriate data into the browser, # and puts the focus on the newtext field return $self->param('OpenThought')->serialize({ fields => $field_data, focus => "newtext", }); }

    In the above, the functions "log_message()" and "get_last_ten()" are hypothetical subs that you would write as part of your chat program. Everything else exists in the framework as seen.

    Thats the skeleton of what you would need in order to make this work using OpenThought. See the demo app that comes with the distribution for an example of an entire working application. There's also a lot of documentation which comes with the module.

    You can find OpenThought at openthought.net. The release thats out now works well, but I highly recommend grabbing the version currently in CVS. There is about to be a new release, and all sorts of neat goodies have been added and/or fixed.

    Good luck!
    -Eric

    --
    Lucy: "What happens if you practice the piano for 20 years and then end up not being rich and famous?"
    Schroeder: "The joy is in the playing."
Re: CGI & HTML Frames
by BUU (Prior) on Aug 15, 2002 at 13:12 UTC
    It all depends on where the 'target' attribute of your form tag is pointing at. The Easiest way is to point the form tag at the script that generates the 'messagebox' in the main window, which will add the entry and cause it to refresh on the viewers screen and so forth, and then just use a bit of javascript magic, something like onSubmit=document.forms[0].chat.value='' Actually the best/easiest thing is to just go get a java one.