pass javascript variable to perl/cgi

  • Thread starter pleaseexplaintome
  • Start date
P

pleaseexplaintome

Hi I have the following perl/cgi script snippet. The goal of this
script is to pass a javascript variable to perl where it can be re-used
later. Any help is appreciated, Thanks

#!/ois/usr/bin/perl -w
use strict;
use CGI qw:)standard);

my $cgi=new CGI;
my $flg=0;
my $datatosave="";

starthtml();

exit 0;

############################################################################################################
sub starthtml {

print "Content-Type: text/html\n\n" ;
print qq(<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN") ;
print qq( "http://www.w3.org/TR/html4/Transitional.dtd"> ) ;
print qq(<html>) ;
print qq(<head>) ;
print qq(<title></title>) ;

print qq(<script language="javascript" type="text/javascript"> ) ;
print qq(function get_textarea() { ) ;

# ALERT DISPLAYS AS EXPECTED
print qq( alert(document.form1.mytextarea.value););

# JAVASCRIPT ERROR OCCURS HERE
# $datatosave = $cgi->param('mytextarea');

# ALERT DOES NOT DISPLAY AS EXPECTED
# print qq( alert($datatosave););

# ALERT DOES NOT DISPLAY AS EXPECTED
# JAVASCRIPT ERROR OCCURS HERE
print qq( alert($cgi->param('mytextarea')););

print qq(} ) ;
print qq(</script> ) ;

print qq(</head><body bgcolor="#FFFFCC">) ;

print qq(<form name=form1 method="post" action="path to test.cgi">)
;

# SUBMIT BUTTON BECAUSE IT WILL ULTIMATELY BE USED TO DISPLAY A
FILE'S CONTENTS
# print qq(<input type="submit" value="Show File">);

# TRIED BOTH SUBMIT AND BUTTON TYPES HERE
print qq(<input type="submit" value="Populate Variable"
onclick="get_textarea()">);

# THE TEXTAREA TO COPY INTO A PERL VARIABLE
print $cgi->textarea(-name=>'mytextarea',
-default=>'test data',
-rows=>5,
-columns=>20);

print qq(</form>) ."\n";
print qq(</body></html>) ;

}
 
B

Bart Van der Donck

Hi I have the following perl/cgi script snippet. The goal of this
script is to pass a javascript variable to perl where it can be re-used
later. Any help is appreciated, Thanks

#!/ois/usr/bin/perl -w
use strict;
use CGI qw:)standard);

my $cgi=new CGI;
my $flg=0;
my $datatosave="";

starthtml();

exit 0;

sub starthtml {

print "Content-Type: text/html\n\n" ;
print qq(<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN") ;
print qq( "http://www.w3.org/TR/html4/Transitional.dtd"> ) ;
print qq(<html>) ;
print qq(<head>) ;
print qq(<title></title>) ;

print qq(<script language="javascript" type="text/javascript"> ) ;
print qq(function get_textarea() { ) ;

# ALERT DISPLAYS AS EXPECTED
print qq( alert(document.form1.mytextarea.value););

# JAVASCRIPT ERROR OCCURS HERE
# $datatosave = $cgi->param('mytextarea');

There is no javascript involved in that line; it is correct Perl code.
# ALERT DOES NOT DISPLAY AS EXPECTED
# print qq( alert($datatosave););

print qq( alert('$datatosave'); );

But you should be aware of single quotes, backslashes and line ends
inside $datatosave.
# ALERT DOES NOT DISPLAY AS EXPECTED
# JAVASCRIPT ERROR OCCURS HERE
print qq( alert($cgi->param('mytextarea')););

$cgi->param('mytextarea') doesn't interpolate, but is shown literally.
You need an extra step:

my $content = $cgi->param('mytextarea');
print qq( alert('$content') );

Same remark about single quotes, backslashes and line ends for
$content.

Hope this helps,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,039
Messages
2,570,375
Members
47,021
Latest member
AleciaMcMa

Latest Threads

Top