Gossamer Forum
Home : General : Perl Programming :

how do i make html realtime with CGI?

Quote Reply
how do i make html realtime with CGI?
Hi guys&gals

Quick question, lets say i have a .cgi program that takes about 4 seconds to run. When you 'submit' via the webpage form (which executes the .cgi program) , it will take about 4 seconds, then displays the results in a new HTML page.

How do i make it ,so once i hit submit via the form, the new web page is displayed instantly, but instead of the 4 second wait for the script to finish executing, the results (of the script) start scrolling and updating on the webpage as the script is running ?

Basically, you're at a html form, you hit submit, another HTML page is created that says for instance "Please wait for results"... then as the scripts starting spitting out the info, you actualyl see the text being spit out in HTML (as if you were running the script from a terminal).

I see it on tons of pages work like that, but i never figured out how to do it. My scripts always execute fully, then displays results. THanks in advance!
Quote Reply
Re: [mtorres] how do i make html realtime with CGI? In reply to
Here's what I do:

Have the html and script part in one subroutine then call another subroutine that displays the html results at the bottom.

Example hitting submit gives you whatever.cgi
--------
{
print <HTML>
Please wait processing order....
</HTML>

perl code to process data (split, join, decode, assign variables - whatever)

&display_results;

}

sub display_results {
print <HTML>
here are your results $field1 $field2 $field3
</HTML>
}
--------------

A "real life" example is taken from the GT dbman script I use is below:

Code:
if ($in{'OrderStatus'} eq "process") {

print qq|
<html>
<head>
<title>$html_title: Record Modified.</title>
|; &script_style_headers; print qq|
<META HTTP-EQUIV = REFRESH CONTENT = "3; URL=$db_script_link_url">
</head>

<body bgcolor="#FFFFFF">
<center>
|; &get_record($in{$db_key}); print qq|

<TABLE HEIGHT="100%"><TD>

<FONT FACE="verdana" SIZE="5" COLOR="#DDDDDD">
Processing order, please wait... </font></td></table>
</center>
</body></html>|;

# strips dd/mm/yyyy down to dd/mm/yy for formatting purposes
$rec{'f03_Est_Closing_Date'} =~ s;\d\d(\d\d)$;$1;;
$rec{'f99_FundDate'} =~ s;\d\d(\d\d)$;$1;;
$rec{'f03_Lock_Expiration'} =~ s;\d\d(\d\d)$;$1;;


## Writes data to text File - sends email/attachment
open(TEXT, ">$in{'LoanNo'}.dta") or die "Nada Can Do \n";
select(TEXT);
print qq|1,"$rec{'LoanNo'}"

ANOTHER 3000 LINES OF CODE GO HERE

The META tag gives the browser 3 seconds to display the message (and allow the script to write out data files) before calling another subroutine of the script via the URL. If the server is running slow (sendmail or something) then the end user sees the please wait message a little longer (couple of seconds).

Last edited by:

Watts: Apr 15, 2004, 9:39 AM
Quote Reply
Re: [Watts] how do i make html realtime with CGI? In reply to
Hrmm, its a smart idea, and i understand what you're doing ... i think..

but here, it looks like it isnt really realtime, its kinda of a workaround, the REFRESH runs another script/sub whatever, and refreshes the page with the new info,

what i'm looking for is for the text to update on the one webpage by itself without recreating a new page.

I really appreciate the time you took, thanks, i'll defintiylly keep this your code example handy
Quote Reply
Re: [mtorres] how do i make html realtime with CGI? In reply to
I can't say I've ever had that problem. All I use is;

Code:
#!/usr/bin/perl

use strict;
use CGI;
my $IN = new CGI;

print $IN->header();

open(LARGEFILE,"<file_to_read.txt") || die $!;
while (<LARGEFILE>) {
print $_ . "<BR>"; # print out each line of the file, and add a <BR> to the end
}
close (LARGEFILE);

print "<BR>Done!!!!";

This would go through the .txt file, and print out each line of it. What browser version are you using? Also, it can make a difference as to what OS your server is running on.. are you on UNIX or NT?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [mtorres] how do i make html realtime with CGI? In reply to
The only way you going to be able to do that is to use javascript and to store your text in a js array or variable and access it that way, or get a faster internet connection.

Bobhttp://totallyfreeads.com.au
Quote Reply
Re: [mtorres] how do i make html realtime with CGI? In reply to
can be a dumb awnser but ...

why u dont do the follow:

your script here.

result here

sleep(4);
print "Location: $new_page\n\n";

sleep will make u site wait 4 secs then the script will redirect it.