print document

Discussion in 'JavaScript' started by aayybb, Sep 15, 2009.

  1. #1
    Hi,

    I have the following code to print contents in an array $items.
    The problem is the part trying to print "special_notes". I don't want the special notes showing when it is viewed on screen but get printed on paper.

    If there is only 1 content in the array then the special_notes get printed fine. But if there are more than 1 content in the array $items, then the special_notes only get printed for the first content in the array. The 2nd content, 3rd content... are missing the special notes. I would like it to be printed with each content in the array. Any suggestion to fix the code?

    Thanks for any help in advance.


    <script language="javascript" type="text/javascript">
    function tablePrint()
    {

    var display_setting="toolbar=yes,location=no,directories=yes,menubar=yes,";
    display_setting+="scrollbars=yes,width=750, height=600, left=100, top=25";
    if(document.getElementById("special_notes").style.display=="none")
    {
    document.getElementById("special_notes").style.display="block";
    }

    var content_innerhtml = document.getElementById("tbl_display").innerHTML;
    var document_print=window.open("","",display_setting);
    document_print.document.open();
    document_print.document.write('<html><head><title>Print articles </title></head>');
    document_print.document.write('<body style="font-family:verdana; font-size:12px;" onLoad="self.print();self.close();" >');
    document_print.document.write(content_innerhtml);
    document_print.document.write('</body></html>');
    document_print.document.close();
    if(document.getElementById("special_notes").style.display=="block")
    {
    document.getElementById("special_notes").style.display="none";
    }
    return false;
    }

    </script>
    <?php
    //Here has the code producing the array $item.
    ?>
    <table>
    <tr>
    <td>
    <?php
    echo '<input type="button" value="Print" onclick="tablePrint();">';
    ?>
    </td>
    <td id="tbl_display" valign="top">

    <?php
    foreach ( $items as $item ) {

    echo "<div style='page-break-after: always'>";
    echo "<p><strong>".$item->title ."</strong></p>";
    echo "<p id='special_notes' style='display:none;'> Note: This document is special, ";
    echo date("m/d/Y"). ".</p>";
    echo "<p>".$item->introtext ;
    echo "</p></div>";
    }

    ?>

    </td>
    </table>
     
    aayybb, Sep 15, 2009 IP