title: Using CGI methods to pass a parameter from JavaScript to Perl script over a web server
body: <p>Wise ones: I am still having extreme troubles with a web application I am building. I am using an APACHE 2 web server. When in localhost, clicking on test.html will pop up a test button which should run the javascript within my .html file.</p>
  
  <p>test.html is as follows:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;script&gt;
  
  
  function loadXMLDoc() {
  var xmlhttp;
  
  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
  }
  else
  {// code for IE6, IE5
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  
  
  xmlhttp.onreadystatechange=function() {
  var str, fileNameVar;
  fileNameVar = "hello";
      if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200){
      str = xmlhttp.responseText;
      alert(str);
      }
  }
  
  xmlhttp.open('GET', 'try.pl?name=fileNameVar' + encodeURIComponent(fileNameVar),false);
  xmlhttp.send();
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;h2&gt;Example&lt;/h2&gt;&lt;/div&gt;
  &lt;button type="button" onclick="loadXMLDoc()"&gt;test&lt;/button&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This web app has worked before when calling a perl script and printing its return value, but this time I am trying to pass a parameter from the javascript to my perl script [try.pl]. As you see I am trying to pass the variable fileNameVar which will hold a string or an integer. </p>
  
  <p>Here is my perl script, try.pl:</p>
  
  <pre><code>#!C:/indigoampp/perl-5.12.1/bin/perl.exe
  use CGI;
  use strict;
  use warnings;
  
  my $cgi = CGI-&gt;new;
  
  my $name = $cgi-&gt;param('name');
  print "Content-type: text/plain\n\n";
  print "$name";
  </code></pre>
  
  <p>So when the button is pushed in my web app, it should simply create an alert  to the user which contains the word "hello".</p>
  
  <p>Though, when I press the button, nothing happens. I don't know what I am doing wrong, as it seems to work for others. Essentially, my web app works but I want to add the functionality of passing a variable from the javascript to the perl script. </p>
  
  <p>NOTE: I do not want to pass "hello" directly in my GET statement. I want it to pass whatever is stored in fileNameVar. Though, in this example, fileNameVar is set to "hello". Thanks for ANY help!</p>