I am using HTML with JavaScript which calls a Perl script over an Apache 2 server.
I want to pass a variable from my JavaScript code to the Perl script it is calling.
I found a common way of doing this using CGI methods. It works successfully but is not quite what I want.
For example, within my JavaScript I have this line:
xmlhttp.open("GET", "try.pl?name=Joe", false);
which calls the Perl script, passing the parameter name
to the script.
Inside my Perl script I have:
#!C:/indigoampp/perl-5.12.1/bin/perl.exe
use CGI qw(:standard);
use strict;
use warnings;
my $query = new CGI;
my $name = $query->param('name');
print "Content-type: text/plain\n\n";
print "$name";
The JavaScript simply prints the result to the screen so Joe
pops up.
The problem is what I want to do is pass a variable. i.e. I have a variable called fileNameVar
in my code which holds a string (the name of a file). I want to pass this variable to the Perl script.
So I want something like:
xmlhttp.open("GET", "try.pl?name=fileNameVar", false);
My variable will be changing: I want it dynamic. I do not want to hard code the filename into the GET
statement as they did with name=Joe
, but how do I do this?
When I try it simply prints fileNameVar
instead of what is stored in fileNameVar
. Any ideas?
All I can find online is the literal (name=Joe
instead of name=variable
). I am very new to web server concepts and any help would be greatly appreciated.