Link to home
Start Free TrialLog in
Avatar of ronan_40060
ronan_40060Flag for United States of America

asked on

Adding JavaScript and DHTML content in a Perl script

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

Avatar of venkateshwarr
venkateshwarr

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
#####################################################################
Avatar of ronan_40060

ASKER

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 ????
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

>>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.
Hello Venkat
I did not get this  what you said   Do you want include html files dynamically??
ASKER CERTIFIED SOLUTION
Avatar of venkateshwarr
venkateshwarr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 :(:(:
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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
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>";
The script looks fine as you posted it.  Are you having any problems?