Solved

Adding JavaScript and DHTML content in a Perl script

Posted on 2004-03-29
11
230 Views
Last Modified: 2013-12-25
My database.pl looks like this

use warnings;
use strict;    
use DBI;      
      
         my $dbh = DBI->connect( 'xxx',
                                 'xxx',
                                 'xxx',  ) || die "Database connection not made: $DBI::errstr\n";
                             
          my $sql=qq{  SELECT count(*) FROM Table };                            
                 
         
         my $sth = $dbh->prepare ($sql)|| die "Prepare failed: $DBI::errstr\n";
         
         $sth->execute() || die "Couldn't execute query:$DBI::errstr\n";

       print "Content-Type: text/html\n\n";
      print "<html><body>";
      print "<BODY leftmargin=0 topmargin=0>";
      print "<table height=\"100%\" width = \"100%\" cellpadding = \"0\" cellspacing = \"0\" border = \"2\" align =       \"center\">";
      print "<tr height=\"5%\">";
      print "<td align = \"center\" colspan =\"2\" bgColor=\"blue\">";
      print "<FONT face=\"Times New Roman\" size=\"5.5\"> OUTPUT </font>";
      print "</td>";
      print "</tr>";
      print "<tr height=\"90%\">";
      print "<td width = \"100%\" colspan = \"2\">";
      
                #####################################################################
                   INSERT CODE HERE

                #####################################################################
      print "<table align=\"center\" bgcolor=\"lightblue\" border=2 bordercolor=\"maroon\" >";
      
      print "<tr>";
      print"<br>";
      print "<td><b>TABLE</b></td>";
      print "</tr>";
      while (my @row= $sth_A->fetchrow_array())
              {
             print "<tr>\n";
             foreach my $my_td (@row) { print "<td>$my_td</td>\n"; }
             print "</tr>\n";
             
            }
      print "</table>";
      print "<br>";
              
         $sth->finish();                                            
         $dbh->disconnect() || die "Failed to disconnect\n";  
       
         print "<tr height=\"5%\">";
      print "<td align = \"center\" colspan =\"2\" bgColor=\"blue\">&nbsp;</td>";
      print "</tr>";
      print "</table>";
      print "</body></html>";
      
----------------------------------------------------------------------------------------------------------------------------------------------

And I need to insert the following code into the area in the above script where its written  INSERT HERE !!!!!!!!!!

the code is follwing

<html>
<head>
<style>
      body{font-family:arial;}
      table{font-size:80%;background:maroon}
      a{color:black;text-decoration:none;font:bold}
      a:hover{color:#black}
      td.menu{background:#f5deb3}
      table.menu
      {
      font-size:100%;
      position:absolute;
      visibility:hidden;
      }
</style>
<script type="text/javascript">
      function showmenu(elmnt)
      {
      document.all(elmnt).style.visibility="visible"
      }
      function hidemenu(elmnt)
      {
      document.all(elmnt).style.visibility="hidden"
      }
</script>
</head>
      
<body>
      <table width="14%">
       <tr bgcolor="#FF8080">
        <td onmouseover="showmenu('Home')" onmouseout="hidemenu('home')">
         <b>Home</b><br />
         <table class="menu" id="accueil" width="100%">
         <tr><td class="menu"><a href="http://localhost/cgi-bin/page1.html">Page1</a></td></tr>
         <tr><td class="menu"><a href="http://localhost/cgi-bin/page2.html">Page 2</a></td></tr>
         <tr><td class="menu"><a href="http://localhost/cgi-bin/page3.html">Page 3</a></td></tr>
         </table>
        </td>
         </tr>
</table>
</body>
</html>

---------------------------------------------------------------------------------------------------------------------------------------------
Any suggestions ?
best regrds

0
Comment
Question by:ronan_40060
  • 4
  • 4
  • 3
11 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10706236
This should work...

#####################################################################
print <<DHTML;

<html>
<head>
<style>
     body{font-family:arial;}
     table{font-size:80%;background:maroon}
     a{color:black;text-decoration:none;font:bold}
     a:hover{color:#black}
     td.menu{background:#f5deb3}
     table.menu
     {
     font-size:100%;
     position:absolute;
     visibility:hidden;
     }
</style>
<script type="text/javascript">
     function showmenu(elmnt)
     {
     document.all(elmnt).style.visibility="visible"
     }
     function hidemenu(elmnt)
     {
     document.all(elmnt).style.visibility="hidden"
     }
</script>
</head>
     
<body>
     <table width="14%">
      <tr bgcolor="#FF8080">
       <td onmouseover="showmenu('Home')" onmouseout="hidemenu('home')">
        <b>Home</b><br />
        <table class="menu" id="accueil" width="100%">
        <tr><td class="menu"><a href="http://localhost/cgi-bin/page1.html">Page1</a></td></tr>
        <tr><td class="menu"><a href="http://localhost/cgi-bin/page2.html">Page 2</a></td></tr>
        <tr><td class="menu"><a href="http://localhost/cgi-bin/page3.html">Page 3</a></td></tr>
        </table>
       </td>
        </tr>
</table>
</body>
</html>


DHTML
#####################################################################
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 10706747
thanks but there is alread print "<head>" and <body> in the perl scrip
writing javascrip and DHtml code starting with print <<DHTML; and ending with ????
0
 
LVL 3

Expert Comment

by:rkosai
ID: 10706773
Looking over your script, you may also be interested in the HTML::Template module as a better alternative.  It allows for code separation by placing the HTML in one file, with loop commands, and the actual Perl script in another file.

Because you're obviously interested in following "best practices," (you've used "strict" and "warnings") this code separation allows for easier maintenance for your script by non-perl people (don't have to worry about $ signs, etc.), and is usually recommended for HTML-heavy scripts such as the one shown.

Documentation for this script is available at: http://search.cpan.org/~samtregar/HTML-Template-2.6/Template.pm
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10707942

>>but there is alread print "<head>" and <body> in the perl script

ronan_40060,

Do you want include html files dynamically?
If not just include the script tags and table tag...
Are you getting any errors??

venkat.
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 10708348
Hello Venkat
I did not get this  what you said   Do you want include html files dynamically??
0
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 300 total points
ID: 10709109
I meant something like this......


use warnings;
use strict;    
use DBI;      
     
         my $dbh = DBI->connect( 'xxx',
                                 'xxx',
                                 'xxx',  ) || die "Database connection not made: $DBI::errstr\n";
                             
         my $sql=qq{  SELECT count(*) FROM Table };                        
                 
         
         my $sth = $dbh->prepare ($sql)|| die "Prepare failed: $DBI::errstr\n";
         
         $sth->execute() || die "Couldn't execute query:$DBI::errstr\n";

      print "Content-Type: text/html\n\n";
     print "<html><body>";
     print "<BODY leftmargin=0 topmargin=0>";
     print "<table height=\"100%\" width = \"100%\" cellpadding = \"0\" cellspacing = \"0\" border = \"2\" align =      \"center\">";
     print "<tr height=\"5%\">";
     print "<td align = \"center\" colspan =\"2\" bgColor=\"blue\">";
     print "<FONT face=\"Times New Roman\" size=\"5.5\"> OUTPUT </font>";
     print "</td>";
     print "</tr>";
     print "<tr height=\"90%\">";
     print "<td width = \"100%\" colspan = \"2\">";
     
                #####################################################################
                ##  CODE INSERTED

print <<DHTML;

<style>
     body{font-family:arial;}
     table{font-size:80%;background:maroon}
     a{color:black;text-decoration:none;font:bold}
     a:hover{color:#black}
     td.menu{background:#f5deb3}
     table.menu
     {
     font-size:100%;
     position:absolute;
     visibility:hidden;
     }
</style>
<script type="text/javascript">
     function showmenu(elmnt)
     {
     document.all(elmnt).style.visibility="visible"
     }
     function hidemenu(elmnt)
     {
     document.all(elmnt).style.visibility="hidden"
     }
</script>
     
     <table width="14%">
      <tr bgcolor="#FF8080">
       <td onmouseover="showmenu('Home')" onmouseout="hidemenu('home')">
        <b>Home</b><br />
        <table class="menu" id="accueil" width="100%">
        <tr><td class="menu"><a href="http://localhost/cgi-bin/page1.html">Page1</a></td></tr>
        <tr><td class="menu"><a href="http://localhost/cgi-bin/page2.html">Page 2</a></td></tr>
        <tr><td class="menu"><a href="http://localhost/cgi-bin/page3.html">Page 3</a></td></tr>
        </table>
       </td>
        </tr>
</table>

DHTML

                #####################################################################
     print "<table align=\"center\" bgcolor=\"lightblue\" border=2 bordercolor=\"maroon\" >";
     
     print "<tr>";
     print"<br>";
     print "<td><b>TABLE</b></td>";
     print "</tr>";
     while (my @row= $sth_A->fetchrow_array())
            {
           print "<tr>\n";
           foreach my $my_td (@row) { print "<td>$my_td</td>\n"; }
           print "</tr>\n";
           
          }
     print "</table>";
     print "<br>";
             
         $sth->finish();                                            
         $dbh->disconnect() || die "Failed to disconnect\n";  
       
         print "<tr height=\"5%\">";
     print "<td align = \"center\" colspan =\"2\" bgColor=\"blue\">&nbsp;</td>";
     print "</tr>";
     print "</table>";
     print "</body></html>";
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 10714134
Hello Venkat
Im getting a nasty error like Can't find string terminator "EOF" anywhere before EOF at print <<DHTML;
I tried evrything but still I am getting the error, dont know why
there is no space after DHTML still I get this error :(:(:
0
 
LVL 3

Assisted Solution

by:rkosai
rkosai earned 200 total points
ID: 10714570
Ronan, there are three things that you could do:

First, if you are running this script on a different platform that you are coding on, you'll want to make sure you're uploading the script in ASCII mode, not Binary mode.

Second, try making
print<<DHTML;
into
print<<"DHTML";


Third, if all else fails, you can always use the q modifier.  Replace "print<<DHTML;" with "print q{" and the DHTML marker with "};"

So it look like:
print q{
<style>
     body{font-family:arial;}
     table{font-size:80%;background:maroon}
     a{color:black;text-decoration:none;font:bold}
     #All the other code inside the DHTML print
};
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10716463

There could be two reasons,

1. there is an exrta space somewhere like
"print <<    DHTML;"

2. the termination of print has space like this...
   
   DTML

it should be

DHTML
0
 
LVL 9

Author Comment

by:ronan_40060
ID: 10716467
hello rkosai
thanks for the help
I did try out print<<"DHTML";   but it did not work and im running the script on the same platform so the only chance for me now is to use print q instead
just go thruouh the code below and correct me if I made any mistake

use warnings;
use strict;    
use DBI;      
     
         my $dbh = DBI->connect( 'xxx',
                                 'xxx',
                                 'xxx',  ) || die "Database connection not made: $DBI::errstr\n";
                             
         my $sql=qq{  SELECT count(*) FROM Table };                        
                 
         
         my $sth = $dbh->prepare ($sql)|| die "Prepare failed: $DBI::errstr\n";
         
         $sth->execute() || die "Couldn't execute query:$DBI::errstr\n";

      print "Content-Type: text/html\n\n";
     print "<html><body>";
     print "<BODY leftmargin=0 topmargin=0>";
     print "<table height=\"100%\" width = \"100%\" cellpadding = \"0\" cellspacing = \"0\" border = \"2\" align =      \"center\">";
     print "<tr height=\"5%\">";
     print "<td align = \"center\" colspan =\"2\" bgColor=\"blue\">";
     print "<FONT face=\"Times New Roman\" size=\"5.5\"> OUTPUT </font>";
     print "</td>";
     print "</tr>";
     print "<tr height=\"90%\">";
     print "<td width = \"100%\" colspan = \"2\">";
     
               

print q {


<style>
     body{font-family:arial;}
     table{font-size:80%;background:maroon}
     a{color:black;text-decoration:none;font:bold}
     a:hover{color:#black}
     td.menu{background:#f5deb3}
     table.menu
     {
     font-size:100%;
     position:absolute;
     visibility:hidden;
     }
</style>
<script type="text/javascript">
     function showmenu(elmnt)
     {
     document.all(elmnt).style.visibility="visible"
     }
     function hidemenu(elmnt)
     {
     document.all(elmnt).style.visibility="hidden"
     }
</script>
     
     <table width="14%">
      <tr bgcolor="#FF8080">
       <td onmouseover="showmenu('Home')" onmouseout="hidemenu('home')">
        <b>Home</b><br />
        <table class="menu" id="accueil" width="100%">
        <tr><td class="menu"><a href="http://localhost/cgi-bin/page1.html">Page1</a></td></tr>
        <tr><td class="menu"><a href="http://localhost/cgi-bin/page2.html">Page 2</a></td></tr>
        <tr><td class="menu"><a href="http://localhost/cgi-bin/page3.html">Page 3</a></td></tr>
        </table>
       </td>
        </tr>
</table>

};

     print "<table align=\"center\" bgcolor=\"lightblue\" border=2 bordercolor=\"maroon\" >";
     
     print "<tr>";
     print"<br>";
     print "<td><b>TABLE</b></td>";
     print "</tr>";
     while (my @row= $sth_A->fetchrow_array())
            {
           print "<tr>\n";
           foreach my $my_td (@row) { print "<td>$my_td</td>\n"; }
           print "</tr>\n";
           
          }
     print "</table>";
     print "<br>";
             
         $sth->finish();                                            
         $dbh->disconnect() || die "Failed to disconnect\n";  
       
         print "<tr height=\"5%\">";
     print "<td align = \"center\" colspan =\"2\" bgColor=\"blue\">&nbsp;</td>";
     print "</tr>";
     print "</table>";
     print "</body></html>";
0
 
LVL 3

Expert Comment

by:rkosai
ID: 10720972
The script looks fine as you posted it.  Are you having any problems?
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question