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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tommo

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

    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\p erl
    use CGI qw(:standard);
    print "Content-type: text/html\n\n";

    print "<HTML>";
    print "<HEAD>";
    print "<TITLE>PRO JECT ONE</TITLE>";

    print "<SCRIPT LANGUAGE = \"JavaScript\"> ";
    var SelectedParamAr ray = new Array();
    var HorseStatValues = new Array();
    function Statistics(coun t, entry)
    {
    alert (count+entry);
    HorseStatValues[count] = entry;
    alert (\"HorseStat = \"+HorseStatVal ues[count]);
    return[false];
    }
    function Select(elemName , numElements)
    {
    var SelectedParamet er =
    document.getEle mentById(elemNa me).options[document.getEle mentById(elemNa me).selectedInd ex].text;
    alert ("Params = "+numElements+" ##Box = "+elemName+"##V alue =
    "+SelectedParam eter);
    SelectedParamAr ray[elemName] = SelectedParamet er;
    alert ("Stored Array Value = "+SelectedParam Array[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><f ont color=\"#FFFFFF \">HORSE
    STATISTICS</font></p>";
    print "<p align=center><s elect name =\"stats\" size=\"5\">";
    $entrycount = 1;
    open(RD,"C:\\Pr ogram Files\\Apache
    Group\\Apache2\ \htdocs\\horse. txt");
    while ($stats = <RD>)
    {
    $total = 0;
    @stats = split(",", $stats);
    $TotalStats = $#stats;
    for ($length=1; $length<=$#stat s; $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><f ont color=\"#FFFFFF \">KEY</font></p>";
    print "<p align=center><s elect name = \"key\" size=\"5\">";

    $paramcount = 1;
    open(MT,"C:\\Pr ogram 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><f ont color=\"#FFFFFF \">PARAMETER S</font></p>";
    print "<br>";
    $int = 0;
    $test = 1;
    @values = (1,2,3,4,5,7,8, 9,10);
    for ($length2=1; $length2<=$Tota lStats; $length2++) {
    print "<font color=\"#FFFFFF \"> $test> </font>";
    print "<select name = $test size=\"5\" onclick = Select('$test',
    '$TotalStats')> ";
    for ($length=0; $length<=$#valu es; $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 SelectedParamAr ray = new Array()\n";
    print "var HorseStatValues = new Array()\n";
    print "function Statistics(coun t, entry)\n";
    print "{\n";
    print "alert (count+entry)\n ";
    print "HorseStatValue s[count] = entry\n";
    print "alert (\"HorseStat = \"+HorseStatVal ues[count])\n";
    print "return[false]\n";
    print "}\n";
    print "function Select(elemName , numElements)\n" ;
    print "{\n";
    print "var SelectedParamet er =
    document.getEle mentById(elemNa me).options[document.getEle mentById(elemNa me).selectedInd ex].text\n";
    print "alert (\"Params = \"+numElements+ \"##Box =
    \"+elemName+\"# #Value = \"+SelectedPara meter)\n";
    print "SelectedParamA rray[elemName] = SelectedParamet er\n";
    print "alert (\"Stored Array Value = \"+SelectedPara mArray[3])\n";
    print "}\n";
    print "</SCRIPT></HEAD>\n";
  • Martin Honnen

    #2
    Re: My Javascript Code will not run from within a Perl/CGI program??



    Tommo wrote:
    [color=blue]
    > 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\p erl
    > use CGI qw(:standard);
    > print "Content-type: text/html\n\n";
    >
    > print "<HTML>";
    > print "<HEAD>";
    > print "<TITLE>PRO JECT ONE</TITLE>";
    >
    > print "<SCRIPT LANGUAGE = \"JavaScript\"> ";[/color]

    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.

    --

    Martin Honnen


    Comment

    • David Dorward

      #3
      Re: My Javascript Code will not run from within a Perl/CGI program ??

      Tommo wrote:[color=blue]
      > 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 ??[/color]
      [color=blue]
      > #!D:\Perl\bin\p erl
      > print "<SCRIPT LANGUAGE = \"JavaScript\"> ";
      > var SelectedParamAr ray = new Array();
      > var HorseStatValues = new Array();
      > function Statistics(coun t, entry)[/color]
      ....

      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.
      [color=blue]
      > 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.[/color]

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

      --
      David Dorward <http://dorward.me.uk/>

      Comment

      • Lee

        #4
        Re: My Javascript Code will not run from within a Perl/CGI program ??

        Tommo said:[color=blue]
        >
        >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>PRO JECT ONE</TITLE>";
        >
        >print "<SCRIPT LANGUAGE = \"JavaScript\"> ";
        >var SelectedParamAr ray = new Array();[/color]

        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.

        Comment

        Working...