无法使用Javascript以html格式打印表格

Not able to print a table in html using Javascript

本文关键字:html 格式 式打印 表格 Javascript      更新时间:2023-09-26

我只是想用javascript打印一个表,但无论出于什么原因,我都无法让它工作。任何建议都将通知

这是我的代码:

<?php 
    include 'connect.php';
    function echoActiveClassIfRequestMatches($requestUri)
    {
        $current_file_name = basename($_SERVER['REQUEST_URI'], ".php");
        if ($current_file_name == $requestUri)
            echo 'class="active"';
    }
    if(isset($_POST['add']))
    {
        header("Location: add_client.php");
        die();
    }
    if(isset($_POST['delete']))
    {
        $DBH = connectDB();
        $trimmed_id = substr($_POST['delete'], 14) ;
        $DBH->exec('DELETE FROM clients where client_ID=' . $trimmed_id);

    }
    if(isset($_POST['modify']))
    {
        $trimmed_id = substr($_POST['modify'], 13);
        header('Location: modify_client.php?client='.$trimmed_id);
        die();
    }
    if(isset($_POST['logout']))
    {
        header("Location: index.php");
        die();
    }
    if(isset($_POST['Print']))
    {

    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Windows and Doors</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <script type="text"/javascript">
function printPage() {
 {
        var DocumentContainer = document.getElementById('printTable');
        var WindowObject = window.open('', "TrackHistoryData", 
                              "width=740,height=325,top=200,left=250,toolbars=no,scrollbars=yes,status=no,resizable=no");
        WindowObject.document.writeln(DocumentContainer.innerHTML);
        WindowObject.document.close();
        WindowObject.focus();
        WindowObject.print();
        WindowObject.close();
    }
    </script>
    <body background="windows.jpg" >

        <nav class="navbar navbar-inverse navbar-static-top">
          <div class="container-fluid">
            <div class="navbar-header">
              <a class="navbar-brand" href="#">Windows and Doors</a>
            </div>
            <div>
              <ul class="nav navbar-nav navbar-right">
                <li <?=echoActiveClassIfRequestMatches("homepage")?>>
                    <a href="home.php">Home</a></li>
                <li <?=echoActiveClassIfRequestMatches("clientspage")?>>
                    <a href="clients.php">Clients</a></li>
                <li <?=echoActiveClassIfRequestMatches("contact")?>>
                    <a href="contact.php">Contact</a></li>
                <li <?=echoActiveClassIfRequestMatches("settings")?>>
                    <a href="settings.php">Settings</a></li>
                <li <?=echoActiveClassIfRequestMatches("reminder")?>>
                <a href="reminder.php">Reminder</a></li>
              </ul>
            </div>
            <form class="navbar-form navbar-left" role="search" method="post">
                <div class="form-group">
                    <input type="text" name="searchvalue" class="form-control" placeholder="Search">
                </div>
             <button type="search" class="btn btn-default"><span class="glyphicon glyphicon-search"></span></button>
            </form> 
          </div>
          <form class="btn pull-right" name="form1" method="post" action="" >
              <input name="logout" type="submit"  value="Logout">
          </form>


        </nav>
        <div class="container" id = "printTable">
            <?php
                    require_once 'connect.php';
                    $DBH = connectDB();
                    session_start();
                    $STH = $DBH->prepare('SELECT * FROM clients');
                    $STH->execute();
            ?>
                <table border= "5px" style="width:1250px" height=200  id="printTable1">
                <th bgcolor="lightgrey"> Select </th>
                <th bgcolor="lightgrey"> ID </th>
                <th bgcolor="lightgrey"> First Name </th>
                <th bgcolor="lightgrey"> Last Name </th>
                <th bgcolor="lightgrey"> Address </th>
                <th bgcolor="lightgrey"> Manufacturer </th>
                <th bgcolor="lightgrey"> Gender </th>   
                <th bgcolor="lightgrey"> Notes </th>
                <th bgcolor="lightgrey"> Delete or modify record</th>
                <?php
                    if(isset($_POST['searchvalue']))
                    {
                        $searchvalue = $_POST['searchvalue'];
                        $STH = $DBH->prepare("SELECT * FROM clients WHERE client_ID LIKE '%" . $searchvalue . "%' OR first_name LIKE '%" . $searchvalue . "%' OR last_name LIKE '%" . $searchvalue . "%' OR address LIKE '%" . $searchvalue . "%' OR gender LIKE '%" . $searchvalue . "%' OR notes LIKE '%" . $searchvalue . "%' OR manufacturer LIKE '%" . $searchvalue . "%'");
                        $STH->execute();
                        while($result = $STH->fetch(PDO::FETCH_ASSOC))
                    {
                        $manufacturer = $result['manufacturer'];
                        $clientID = $result['client_ID'];
                        $fname = $result['first_name'];
                        $lname = $result['last_name'];
                        $address = $result['address'];
                        $gender = $result['gender'];
                        $notes = $result['notes'];
                ?>
                        <tr bgcolor="lightblue">
                            <td bgcolor="lightblue">
                                <?php echo $clientID ?>
                            </td>
                            <td bgcolor="lightblue">
                                <?php echo $fname ?>
                            </td>
                            <td bgcolor="lightblue">
                                <?php echo $lname ?>
                            </td>
                            <td bgcolor="lightblue">
                                <?php echo $address ?>
                            </td>
                            <td bgcolor="lightblue">
                                <?php echo $manufacturer?>
                            </td>
                            <td bgcolor="lightblue">
                                <?php echo $gender ?>
                            </td>
                            <td bgcolor="lightblue">
                                <?php echo $notes ?>
                            </td>
                            <td bgcolor="lightblue">
                                <form action= "" method="post" role="form">
                                <input type = "submit" name ="delete"  class="btn btn-inverse" value="Delete Client#<?php echo $clientID ?>" >
                                <input type = "submit"  name = "modify" class="btn btn-inverse" value="modifyClient#<?php echo $clientID ?>" >
                            </form>
                            </td>
                        </tr>
                        <?php 
                    }   
                    }
                    else
                    {   
                        $STH = $DBH->prepare('SELECT * FROM clients');
                        $STH->execute();
                    while($result = $STH->fetch(PDO::FETCH_ASSOC))
                    {
                        $manufacturer = $result['manufacturer'];
                        $clientID = $result['client_ID'];
                        $fname = $result['first_name'];
                        $lname = $result['last_name'];
                        $address = $result['address'];
                        $gender = $result['gender'];
                        $notes = $result['notes'];
                ?>
                        <tr bgcolor="lightblue">
                            <td><input type="checkbox" /></td>
                            <td bgcolor="lightblue">
                                <?php echo $clientID ?>
                            </td>
                            <td bgcolor="lightblue">
                                <?php echo $fname ?>
                            </td>
                            <td bgcolor="lightblue">
                                <?php echo $lname ?>
                            </td>
                            <td bgcolor="lightblue">
                                <?php echo $address ?>
                            </td>
                            <td bgcolor="lightblue">
                                <?php echo $manufacturer?>
                            </td>
                            <td bgcolor="lightblue">
                                <?php echo $gender ?>
                            </td>
                            <td bgcolor="lightblue">
                                <?php echo $notes ?>
                            </td>
                            <td bgcolor="lightblue">
                                <form action= "" method="post" role="form">
                                <input type = "submit" name ="delete" class="btn btn-inverse" value="Delete Client#<?php echo $clientID ?> ">
                                <input type = "submit"  name = "modify" class="btn btn-inverse" value="modifyClient#<?php echo $clientID ?>" >
                            </form>
                            </td>
                        </tr>
                        <?php 
                    }   }
                        ?>
                </table>
             <form action= "" id="test" method="post" role="form">
            <button name="add" type="submit" class="btn btn-default">Add</button>

            <input type ="submit" value="Print" onclick="printPage('printTable')"/>
            <button name="SummaryReport" type="submit" class="btn btn-default">Summary Report</button>
            </form>

            <div class = "navbar navbar-default navbar-fixed-bottom"/>
                <div class = "container">
                    <p class = "navbar-text">Site built by Super Red</p>
            </div>

    </body>
</html>

我不知道是我的打印功能还是其他一些常见错误。任何建议都将不胜感激!

<script language="javascript" type ="text/javascript" >
function printTbl() {
    var TableToPrint = document.getElementById('ctl00_ContentPlaceHolder1_FormView1');
    newWin = window.open("");
    newWin.document.write(TableToPrint.outerHTML);
    newWin.print();
    newWin.close();
    return false;
}