1

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.

Borodin
  • 126,100
  • 9
  • 70
  • 144
Amanda Kirk
  • 78
  • 2
  • 10
  • 1
    You can concatenate strings in JS with the `+` operator, e.g. `"...name=" + fileNameVar`. But you should consider encoding the value first. – amon Jun 14 '13 at 19:15
  • http://stackoverflow.com/questions/282647/does-perl-have-php-like-dynamic-variables may help? (requires you to turn off `use strict` because variable variable names are usually a Bad Idea; use a hash instead.) – Wooble Jun 14 '13 at 19:21

2 Answers2

5

You should append an encoded version of the string to the URL without the query parameter value.

A call to encodeURIComponent encodes as hex numbers characters that may otherwise be illegal within a URL.

The + operator concatenates strings.

So you want

xmlhttp.open('GET', 'try.pl?name=' + encodeURIComponent(fileNameVar), false);
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • 1
    Alternate option: use jQuery, and let jQuery do the encoding. – John Dvorak Jun 14 '13 at 19:37
  • 1
    @JanDvorak: That's an [*"I wouldn't start from here if I were you"*](http://simonkidd.wordpress.com/2010/08/12/if-i-were-you-i-wouldnt-start-from-here/) kind of answer :) – Borodin Jun 14 '13 at 19:39
  • To clarify, within my JavaScript if fileNameVar stored a value say the integer 2 the above would pass a parameter called name and within my peel code I could use cgi-> param('name') to get the integer stored in name? This is what I want to be able to do. – Amanda Kirk Jun 15 '13 at 18:26
-5

I don't think you can directly pass a javascript variable to a perl script. You could save the contents of the javascript variable in a cookie and then read that cookie from within the perl CGI script. Check this out: http://perldoc.perl.org/CGI/Cookie.html. Or, you could set up a session to retain state between javascript and the perl script.

  • The other answer is *still* passing the content of the variable to the perl script. It's URL encoded, which is good, but I thought you didn't want to directly pass the content of the variable. – user2372262 Jun 14 '13 at 19:56
  • Previously, my code was passing LITERALLY fileNameVar I.e the string FileNameVar was stored in name so when I got the contents of name in my perl script it didn't get the VALUE stored in fileNameVar essentially if I have a variable on my perl script that stores a value say the integer 2 I want to pass this variable to my perl script and within my perl script using cgi->param('varname') get whatever's on that variable....sorry it's hard to explain but super simple – Amanda Kirk Jun 15 '13 at 18:31
  • Yes, that is much simpler. My response is getting hammered b/c I thought you were asking something more difficult to achieve. – user2372262 Jun 15 '13 at 22:35
  • sorry for my poor articulation! – Amanda Kirk Jun 16 '13 at 17:33