My Javascript Code will not run from within a Perl/CGI program ??

T

Tommo

Hello All,
I am a still learning so be easy on me. I am trying to get
some code to work that is using JS and Perl/CGI, I am using AS Perl
and an Apache Server on XP as the webserver. Can anyone take a look at
the code and tell me why I am getting Apache Server errors in the code
below (Part A), when I modify the code as shown in part B the errors
go but the JS does not run ??

Part A.

#!D:\Perl\bin\perl
use CGI qw:)standard);
print "Content-type: text/html\n\n";

print "<HTML>";
print "<HEAD>";
print "<TITLE>PROJECT ONE</TITLE>";

print "<SCRIPT LANGUAGE = \"JavaScript\">";
var SelectedParamArray = new Array();
var HorseStatValues = new Array();
function Statistics(count, entry)
{
alert (count+entry);
HorseStatValues[count] = entry;
alert (\"HorseStat = \"+HorseStatValues[count]);
return[false];
}
function Select(elemName, numElements)
{
var SelectedParameter =
document.getElementById(elemName).options[document.getElementById(elemName).selectedIndex].text;
alert ("Params = "+numElements+"##Box = "+elemName+"##Value =
"+SelectedParameter);
SelectedParamArray[elemName] = SelectedParameter;
alert ("Stored Array Value = "+SelectedParamArray[3]);
}
print "</SCRIPT></HEAD>\n";


print "<body bgcolor = \"#2000E2\">";
print "<p align=center><b><font face=Verdana color=\"#FFFF80\"
size=\"4\"> PROJECT ONE</font></b></p>";
print "<p align=center><font color=\"#FFFFFF\">HORSE
STATISTICS</font></p>";
print "<p align=center><select name =\"stats\" size=\"5\">";
$entrycount = 1;
open(RD,"C:\\Program Files\\Apache
Group\\Apache2\\htdocs\\horse.txt");
while ($stats = <RD>)
{
$total = 0;
@stats = split(",", $stats);
$TotalStats = $#stats;
for ($length=1; $length<=$#stats; $length++)
{
$total = $total + @stats[$length];
}
$HorseStatsHash{$total} = $stats;
$entrycount++;
}
close(RD);
foreach $total (sort {$b<=>$a} keys %HorseStatsHash)
{
print "<option value = $HorseStatsHash{$total}>
$HorseStatsHash{$total} = $total</option>";
}
print "</select></p>";
#
#
print "<br>";
print "<p align=center><font color=\"#FFFFFF\">KEY</font></p>";
print "<p align=center><select name = \"key\" size=\"5\">";

$paramcount = 1;
open(MT,"C:\\Program Files\\Apache
Group\\Apache2\\htdocs\\params.txt");
while ($keys = <MT>)
{
print "<option value=$keys> $paramcount = $keys</option>";
$paramcount++;
}
print "</select></p>";
close(MT);
print "<p align=center><font color=\"#FFFFFF\">PARAMETERS</font></p>";
print "<br>";
$int = 0;
$test = 1;
@values = (1,2,3,4,5,7,8,9,10);
for ($length2=1; $length2<=$TotalStats; $length2++) {
print "<font color=\"#FFFFFF\"> $test> </font>";
print "<select name = $test size=\"5\" onclick = Select('$test',
'$TotalStats')>";
for ($length=0; $length<=$#values; $length++) {
print "<option value=@values[$length]>
@values[$length]</option>";
}
print "</select></td>";
if ($int == 13) {
$int = 0;
print "<br>";
print "<br>";
}
$int++;
$test++;
}
print "</body>";
print "</HTML>";




If I change the JS part of the code to look like like below then I do
not get any errors even from command line, the page loads ok but I do
not get any of the Alert popups so I presume that the JS is not being
run ???

Part B.

print "<SCRIPT LANGUAGE = \"JavaScript\">\n";
print "var SelectedParamArray = new Array()\n";
print "var HorseStatValues = new Array()\n";
print "function Statistics(count, entry)\n";
print "{\n";
print "alert (count+entry)\n";
print "HorseStatValues[count] = entry\n";
print "alert (\"HorseStat = \"+HorseStatValues[count])\n";
print "return[false]\n";
print "}\n";
print "function Select(elemName, numElements)\n";
print "{\n";
print "var SelectedParameter =
document.getElementById(elemName).options[document.getElementById(elemName).selectedIndex].text\n";
print "alert (\"Params = \"+numElements+\"##Box =
\"+elemName+\"##Value = \"+SelectedParameter)\n";
print "SelectedParamArray[elemName] = SelectedParameter\n";
print "alert (\"Stored Array Value = \"+SelectedParamArray[3])\n";
print "}\n";
print "</SCRIPT></HEAD>\n";
 
M

Martin Honnen

Tommo said:
I am a still learning so be easy on me. I am trying to get
some code to work that is using JS and Perl/CGI, I am using AS Perl
and an Apache Server on XP as the webserver. Can anyone take a look at
the code and tell me why I am getting Apache Server errors in the code
below (Part A), when I modify the code as shown in part B the errors
go but the JS does not run ??

Part A.

#!D:\Perl\bin\perl
use CGI qw:)standard);
print "Content-type: text/html\n\n";

print "<HTML>";
print "<HEAD>";
print "<TITLE>PROJECT ONE</TITLE>";

print "<SCRIPT LANGUAGE = \"JavaScript\">";

If you are getting Apache errors then this newsgroup is not the right
place to solve that.
As for writing chunks of HTML with Perl I suggest to use the heredoc
notation e.g.

print <<EOF;
<script type="text/javascript">
function () {
...
}
</script>
EOF

as that is much easier than printing each line with a statement.

Try that to ease your task of outputting HTML.
If you get script errors then try to make a short example and present
only the output of the Perl CGI (do view source in the browser) here in
this group.
 
D

David Dorward

Tommo said:
I am a still learning so be easy on me. I am trying to get
some code to work that is using JS and Perl/CGI, I am using AS Perl
and an Apache Server on XP as the webserver. Can anyone take a look at
the code and tell me why I am getting Apache Server errors in the code
below (Part A), when I modify the code as shown in part B the errors
go but the JS does not run ??
#!D:\Perl\bin\perl
print "<SCRIPT LANGUAGE = \"JavaScript\">";
var SelectedParamArray = new Array();
var HorseStatValues = new Array();
function Statistics(count, entry)
....

I've never seen a Perl interpreter that could cope with JavaScript. You
either need a server side JavaScript interpreter, or to print the code to
the client so the browser can run it.
If I change the JS part of the code to look like like below then I do
not get any errors even from command line, the page loads ok but I do
not get any of the Alert popups so I presume that the JS is not being
run ???

Part B.

<snip>

No way am I going to try to interpret Perl code in my head to work out what
JavaScript code is being produced.

If you are having trouble with the JavaScript, the post the JavaScript that
is giving the problem (and be aware that you can't execute JavaScript
functions from Perl without writing a JavaScript function call to the
browser, and that you can't execute Perl subs from JavaScript without
making a new http request - ditto accessing each others variables.).
 
L

Lee

Tommo said:
Hello All,
I am a still learning so be easy on me. I am trying to get
some code to work that is using JS and Perl/CGI, I am using AS Perl
and an Apache Server on XP as the webserver. Can anyone take a look at
the code and tell me why I am getting Apache Server errors in the code
below (Part A), when I modify the code as shown in part B the errors
go but the JS does not run ??

Part A.

#!D:\Perl\bin\perl
use CGI qw:)standard);
print "Content-type: text/html\n\n";

print "<HTML>";
print "<HEAD>";
print "<TITLE>PROJECT ONE</TITLE>";

print "<SCRIPT LANGUAGE = \"JavaScript\">";
var SelectedParamArray = new Array();

That last line, and every line up until your next
"print" are all still being interpretted by the Perl engine,
and since they're Javascript, and not Perl, Apache is going
to complain.

In the output of your part B, the line:
return[false]
is a syntax error. It should be:
return false;

You should be using the onChange event handler, instead of
the onClick handler of the Select object.
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top