View Full Version : perl cgi onClick button
lara123
03-18-2005, 12:30 PM
Hi, I have a form which has multiple run_buttons,
The form looks something like :
script_name1 | system1 | run_button | result(pass/fail)
script_name2 | system2 | run_button | result(pass/fail)
when the user clicks on the run_button i need to execute script on the system associated with that run_button ... i have a script "run.cgi" that does that ... then i need ot post the result of the script in the result column.
My problem starts here. The run_buttons i have are onClick buttons, so i have ot invoke javascript when the user clicks on the run_button, the javascript in turn opens the url that to "run.cgi". Now i dont know how to get the result from that page back to my form page. Is there a way that when the onClick button is clicked, i ditectly call some perl-cgi code, or if i have to call a java script function, can that javascript call the perl function, without opening a new window???
thanks, lara
mlseim
03-18-2005, 01:49 PM
You can have the Perl script re-display the same form page
after it executes. But, the only way to get the results back
to the page would be to use cookies. Cookies can be written
by Perl and read by a javascript on your form page. The
javascript can put the cookie values into the form text boxes.
Here's an example where Perl writes cookies and Javascript
reads them to display on a form. Notice how when you set
the cookies, the Perl script runs and then re-displays the
page "cook.htm".
http://www.catpin.com/cook.htm
Maybe if you explain more about why the tests need to be
executed with a button ... it's possible to have a CRON
running that automatically runs your tests every couple of
hours, or days, whatever. The results could be written
into a "log file". The log could be viewed as a webpage.
A "CRON" is a system that runs script automatically from
the server. Not all webhosts allow CRON jobs, but yours
might.
EDIT
---------
I've been reading your posts in the Javascript section.
In order to make this work ... with Perl setting cookies
and returning to the form, you need to be able to make
changes to each Perl script that you are testing. You
didn't mention if you had access to the scripts and were
able to change them. Within each script you are testing,
you will have to have them set cookies and return to the
original form (the one that has the option buttons).
.
lara123
03-21-2005, 06:27 AM
Hi,
Sorry for the late response.
I think this solves the problem. I have access tot the scripts etc. In fact, all i do is, on clicking the run button, do some code and store the results in a file. When i was viewing the coookie example link you have sent, there is in the beginning:
The first link (Set the Cookies), goes to the Perl script and sets
two cookies ... avg (average) and med (median).
After the script sets them, it just returns back to this page.
Set the Cookies
----
well that is what i need to know. How can i access a link, and return to the same page. "set the cookies" does excatly that. It calls a function without opening any new windows and the user continues to see the form,
i will appreciate any more input, and thanks for looking into it.
lara.
mlseim
03-21-2005, 02:07 PM
----
It calls a function without opening any new windows and the user continues to see the form,
i will appreciate any more input, and thanks for looking into it.
lara.
Well, actually it appears like it does. The Perl script runs and
goes back to the location of the form. In some instances, like
a dial-up connection, the user may see a page refresh of the
page.
The first step is to upload a copy of a cookie library called "cookie.lib".
This is a free script that helps make using cookies easy.
Get the script here and upload it into your cgi-bin in ASCII mode, CHMOD to 755.
http://www.scriptarchive.com/download.cgi?s=cookielib&c=txt&f=cookie%2Elib
The only part you need to change in the script is the domain name
$Cookie_Domain = '';
Put your domain in like: $Cookie_Domain = 'airtravel.com';
Once you get the script uploaded, I will post the next part.
(setting the cookies ... and the javascript).
--max--
lara123
03-22-2005, 10:45 AM
Ok, I got the cookie.lib file.
Thanks again
mlseim
03-22-2005, 02:24 PM
OK ...
Now you need to alter each Perl script that you are running.
In each script, add this line somewhere near the beginning of the script:
require 'cookie.lib';
Then, (although I can't see your script), I'm assuming you have some
sort of printout that says "PASS" or "FAIL" or something like that.
Anyhow, instead of displaying anything at the end of the script,
comment out the printing lines with ## in front of each line.
Put these lines there instead:
$status = "PASS"; # $status is whatever variable you have for PASS/FAIL.
&SetCookies('script1','$status');
# enter the URL of the form that called this script:
print "Location: http://www.mysite.com/myform.html\n\n";
====================================
In each script, you will give it a different cookie name like:
&SetCookies('script1','$status');
&SetCookies('script2','$status');
&SetCookies('script3','$status');
This will allow the Javascript to read each cookie from each script,
and display a PASS or FAIL.
If you have trouble, you're going to have to save your scripts as text
files (.txt) and attach them on this forum so we can see them.
---------------------------
The Javascript Part ....
This is where you'll be adding Javascripting to the page that has the
HTML form.
I was going to post the Javascript to add, but I need to see your form
in order to see what values you're using for fields. Give us a link to
the form, or post it here.
---------------------------
lara123
03-23-2005, 05:32 AM
Here's the main perl-cgi script which prints the form as a table:
#!/usr/bin/perl -w
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use Fcntl qw(:flock :seek);
print header;
#Java Script Code:
my $JSCRIPT=<<END;
var commandURL = "page24.cgi?";
var commandURL2 = "page25.cgi?";
function sendURL(url)
{
window.open (url);
}
function runScript(element1, element2, element3) {
//document.form1.result1.value = "pass/fail";
var params = "scriptname=" + element1;
params += "&clientname=" + element2;
params += "&pathname=" + element3;
sendURL (commandURL + params);
return false;
}
function runScript2(element1) {
//alert("here");
var params = "file=" + element1;
sendURL (commandURL2 + params);
return false;
}
END
print start_html (-title=>'USER SELECTION CONFIRMED',
-script=>$JSCRIPT);
my @script_names; #store the script names
my @client_names; #stores the client names
my @paths; #path address of scripts
my $outfile;
my $i;
my $size; #size of all arrays
&get_script_client_names;
&store_info;
&display;
print end_html;
sub get_script_client_names {
my @param_names; #store the param_names
# the hidden values passed on to it
my @variables = param('name');
my $count = scalar(@variables);
$count = $count -1; #adjustment for filename
#IMPORTANT
# $outfile=$variables[$count];
$outfile ="/usr/PBT/scen";
$size = $count/3;
$i=0;
#variables is divides into 2 parts, array of scripts names
$i=0;
while ($i < $size)
{
$script_names[$i]= $variables[$i];
++$i;
}
$i = $size; # follows the variables
my $pointer =0;
# array of param names
while ($i < ($size*2))
{
$param_names[$pointer]= $variables[$i];
++$i;
++$pointer;
}
$i = ($size*2);
$pointer =0;
# array of param names
while ($i < $count)
{
$paths[$pointer]= $variables[$i];
++$i;
++$pointer;
}
# store the choices made in array client_names
$i =0;
foreach my $value (@param_names) {
$client_names[$i]= param($value);
$i++ ;
}
} #get script clinet names
sub start_func{
my ($passin,$passin2,$passin3) = @_;
button (-name=>'start',-value=>'Start',
-onClick=>"runScript('$passin','$passin2','$passin3')");
}
sub result_func {
my $code = @_;
my $id = "result".$code;
textfield (-name=>$id, -size=>6);
}
sub log_func {
my ($script,$client) = @_;
my $file = "/usr/local/apache/cgi-bin/log.txt_".$script."_".$client;
button (-name=>'log',-value=>'Log_File',
-onClick=>"runScript2('$file')"); }
sub error_func{
my ($script,$client) = @_;
my $file = "/usr/local/apache/cgi-bin/error.txt_".$script."_".$client;
button (-name=>'error',-value=>'Error_File',
-onClick=>"runScript2('$file')"); }
sub display {
#again remove extension from script to display
$i=0;
my @script_names_displayed;
my @temp;
while ($i<$size) {
@temp= split(/\./,$script_names[$i]);
$script_names_displayed[$i] = $temp[0];
$i++;
}
print start_form (-name=>'form1');
my @heading = ("Scrip Name", "Client", "Status", "Result", "Log File", "Error_File" );
my @rows;
$i =0;
while ($i<$size) {
push (@rows, td($script_names_displayed[$i]).
td($client_names[$i]).
td(&start_func($script_names[$i],$client_names[$i],$paths[$i])).
td(&result_func($i)).
td(&log_func($script_names[$i],$client_names[$i])).
td(&error_func($script_names[$i],$client_names[$i]))
);
$i++;
}
print table( {-id=>'table1', -border=>undef, -width=>'100%'},
caption(b("THE SCENARIO $outfile THAT YOU CREATED")),
Tr ([ th (\@heading), @rows ])
);
print end_form;
}
sub dienice {
my($errmsg) = @_;
print header;
print start_html("Error");
print "<h2> Error</h2>\n";
print "<p>$errmsg</p>\n";
print end_html;
exit;
}
i hope it makes things clearer.
//document.form1.result1.value = "pass/fail";
is the line where i need to print the cooie value which i set in the script page24.cgi
lara123
03-23-2005, 05:52 AM
This is file page24.cgi which remotely executes a script. It print the results "pass/fail" i need to post these results back on page23.cgi. Infact i do not wish the user to see this page24.cgi at all. Is there a way I could change my page23.cgi, so it does not open this page24, just calls it, and returns bac to the form, like "set the cookies" on your original post did??
#!/usr/bin/perl -w
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
my $JSCRIPT=<<END;
var commandURL = "page25.cgi?";
function sendURL(url)
{
window.open(url,'myWin', "toolbar=yes, resizable=yes, location=yes, width=600","height=500");
}
function runScript(element1) {
var params = "file=" + element1;
sendURL (commandURL + params);
return false;
}
END
print header;
print start_html (-title=>'REMOTE EXECUTION', -script=>$JSCRIPT);
$scriptname = param('scriptname');
$clientname = param('clientname');
$pathname = param('pathname');
$exec_script = $pathname."/".$scriptname ;
print "Runiing script $exec_script on $clientname";
print hr;
$file = "log.txt";
$efile = "error.txt";
$store_file = $file."_".$scriptname."_".$clientname;
$error_file = $efile."_".$scriptname."_".$clientname;
system "touch $store_file";
system "chmod 777 $store_file" ;
system "touch $error_file";
system "chmod 777 $error_file";
$return_value=system"(rsh -l root $clientname $exec_script) >$store_file 2>$error_file ";
print "Result is: ";
$pass = font({color=>green},"PASS");
$fail = font({color=>red},"FAIL");
if ( $return_value == 0)
{ print $pass;}
else
{print $fail;}
print end_html;
mlseim
03-23-2005, 02:24 PM
wow ... not quite as straight-forward as I had imagined ...
Attached are the two files in text format.
In "page24.txt", the very bottom line...
I don't know what the name of the main form script is called.
You need to put that in there so it can return to that script.
In each script page25, page26 etc, put the 'cookie.lib' line
at the top and change the cookie value in the PASS FAIL line.
You can write as many cookies as you want. When the
browser closes, the cookies are erased.
In "main_script.txt", You'll see the javascript function in the
<head> for reading the cookie, and ONE cookie line at the
end of the form. For now, there's only one cookie being
read "page24". If it works, you'll have to duplicate the
line for each textbox and cookie value you have set from
the correct script (page25, page26, etc).
For now, I'm just curious to see if it works.
I have no way of testing it.
Also, make sure that in "cookie.lib", you've put in your
domain name. $Cookie_Domain = 'yourname.com';
.
lara123
03-24-2005, 06:01 AM
Hi,
Well first of all you do know the name of my mainform ..it is page23.cgi
I did the changes as you recommended, but things haven't worked out nicely. At the end og page 24.cgi i write:
print "Location: http://157.227.25.213/cgi-bin/page23.cgi\n\n";
Now on my mainform (page23.cgi) when i click on the start button, i see no new pages which is good, but my cookies don't appear, which isn't good.
hmmm, well thank you for the help, anyhow. I shall update you if i figure out what went wrong where.
lara123
03-24-2005, 06:45 AM
I believe it has something to do with the domain, i am not working in a domain, but a work group:
I have set the domain to the gateway address,..
anyhow i realized that CGI.pm also has a cookie function, which sets the cookie for me and i am also able to retrieve it.
Than i set it the way you do:
print "<script> document.form1.result1.value = cookie_value </script> " and it works beautifully :-)
Thanks a ton for the all in the inputs and help, at last it is working and i am really happy.
mlseim
03-24-2005, 02:00 PM
Glad you got it working ...
There's so many different operating systems with various modules,
it's always hard to tell what your server offers.
But, it looks like you were able to get enough of an idea to
figure it out. That's all that counts.. :thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.