Help with Perl and Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KimberlyChang
    New Member
    • Dec 2006
    • 2

    Help with Perl and Javascript

    Hello, I am having an issue that is driving me nuts and probably has a simple resolution,

    I am storing Java script code in a string example

    $javaSC pulls code from a database

    $javaSC = "<script type="text/javascript">
    <!--
    xxx
    //--></script>

    the problem is when displayed for the user to copy it only shos this

    <script type="text/javascript">
    </script>

    this part

    <!--
    xxx
    //-->

    is missing.


    Please help

    Kimberly
  • GunnarH
    New Member
    • Nov 2006
    • 83

    #2
    Show us a short but complete script that demonstrates the problem you are having. Then somebody may be able to help you get it right.

    Comment

    • KimberlyChang
      New Member
      • Dec 2006
      • 2

      #3
      Below is hhort and sweet and duplicates the error Thank you for the help


      #!/usr/bin/perl -w

      @widgetformhead = <<"HTML";
      <form method="post">
      <textarea name="snippetso urce" cols="50" class="required " id="snippet[source]"/>
      </textarea>
      <input type="submit" class="primaryA ction" id="submit-wf_TestForm" value="submit"/>
      <input name="entered" type="hidden" value="yes">
      </form>
      HTML


      ###get form values

      if ($ENV{'REQUEST_ METHOD'} eq 'GET') {
      @pairs = split(/&/, $ENV{'QUERY_STR ING'});
      } elsif ($ENV{'REQUEST_ METHOD'} eq 'POST') {
      read(STDIN,$buf fer,$ENV{'CONTE NT_LENGTH'});
      @pairs = split(/&/, $buffer);
      if ($ENV{'QUERY_ST RING'}) {
      @getpairs = split(/&/,$ENV{'QUERY_ST RING'});
      push (@pairs, @getpairs);}
      }

      foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);
      $name =~ tr/+/ /;
      $value =~ tr/+/ /;
      $name =~ s/%(..)/pack("C", hex($1))/eg;
      $value =~ s/%(..)/pack ("C", hex($1))/eg;
      $value =~ s/<!--(.|\n)*-->//g;

      if ($in{$name}){$i n{$name}.=", $value";}
      else{$in{$name} = $value;}
      }

      if ($in{'entered'} ne "yes"){
      print "Content-Type: text/html\n\n";
      print @widgetformhead ;
      }
      else
      {
      print "Content-Type: text/html\n\n";
      print $in{'snippetsou rce'};
      }

      Comment

      • GunnarH
        New Member
        • Nov 2006
        • 83

        #4
        Originally posted by KimberlyChang
        $value =~ s/<!--(.|\n)*-->//g;
        That line is the culprit.

        You'd better use the CGI module for parsing the form data:
        Code:
        #!/usr/bin/perl
        use strict;
        use warnings;
        use CGI;
        
        my @widgetformhead = <<"HTML";
        <form method="post">
        <textarea name="snippetsource" cols="50" class="required" id="snippet[source]"/>
        </textarea>
        <input type="submit" class="primaryAction" id="submit-wf_TestForm" value="submit"/>
        <input name="entered" type="hidden" value="yes">
        </form>
        HTML
        
        my $cgi = CGI->new;
        my %in = $cgi->Vars;
        print $cgi->header;
        
        if ( $in{entered} ne 'yes' ) {
            print @widgetformhead;
        } else {
            $in{snippetsource} = CGI::escapeHTML( $in{snippetsource} );
            print "<pre>$in{snippetsource}</pre>";
        }

        Comment

        Working...