473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

pass javascript variable to perl/cgi

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>) ;

}

Dec 9 '06 #1
2 4849
pl***************@yahoo.com wrote:
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,

--
Bart

Dec 9 '06 #2
Bart, a BIG!!! thank you to you.

Dec 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nath | last post by:
Please help!? I am new to writing html, javascript, pretty new to MySQL but quite proficient at writing Perl and i'm a quick learner. I am building a database driven website and i am a little...
3
by: stahl.karl | last post by:
I have a CGI/Perl program that returns a string output. Is it possible to get this into a Javascript variable, where the name of the variable is defined in the Javascript and not in the Perl code?...
2
by: zakhirn | last post by:
Hello, I have the follow XML, XSL, and HTML file with javascript: cd.html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
2
by: kid | last post by:
Hi i need help i want to pass javascript variable to php. here's my sample code its not working. please someone help me.... <select name='dbPages' OnChange='getLayoutID(this.value)'> <option...
3
by: sasimca007 | last post by:
how to assign the perl variable to javascript variable?
2
oll3i
by: oll3i | last post by:
How do I pass javascript vars to php? Thank You
3
by: sasimca007 | last post by:
Hello friends, IN modperl we write perl with html,javascript and etc. when we are writing perl code in the middle of javascript and if we want to assign a javascript variable...
7
by: Arsale | last post by:
I am trying to pass a javascript variable to php on javascript generated page, but on the new page I got only "Array". Where did I go wrong? On first page, i've got image map with areas....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.