• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

How to Print Document in PHP and JavaScript

3serve

Level-Up Legend
3 Rep
0
0
0
Rep
0
3 Vouches
0
0
0
Vouches
0
Posts
107
Likes
166
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
In this tutorial we will tackle on How To Print Document using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily learn its syntax. It is mostly used by a newly coders for its user-friendly environment.

So Let's do the coding.

Before we started:


Creating Database

Open your database web server then create a database naming "db_print". After that, click Import then locate the database file inside the folder of the application then click ok.

2018-06-12_14_49_03-facebook.png


Or, you can copy and paste the SQL script below in SQL page of the database.

  1. CREATE

    TABLE

    `product`

    (
  2. `product_

    id`

    int

    (

    11

    )

    NOT

    NULL

    PRIMARY KEY

    AUTO_INCREMENT

    ,
  3. `pname`

    varchar

    (

    50

    )

    NOT

    NULL

    ,
  4. `price`

    int

    (

    12

    )

    NOT

    NULL

    ,
  5. `quantity`

    int

    (

    5

    )

    NOT

    NULL

    ,
  6. `date_

    added`

    varchar

    (

    50

    )

    NOT

    NULL


  7. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  8. INSERT

    INTO

    `product`

    (

    `product_

    id`

    ,

    `pname`

    ,

    `price`

    ,

    `quantity`

    ,

    `date_

    added`

    )

    VALUES


  9. (

    1

    ,

    'Bear Brand'

    ,

    14

    ,

    200

    ,

    '2018-06-12'

    )

    ,
  10. (

    2

    ,

    'Fresh Milk'

    ,

    90

    ,

    500

    ,

    '2018-06-12'

    )

    ,
  11. (

    3

    ,

    'Tobleron'

    ,

    60

    ,

    100

    ,

    '2018-06-12'

    )

    ,
  12. (

    4

    ,

    'Avocado'

    ,

    50

    ,

    50

    ,

    '2018-06-12'

    )

    ;

Creating the database connection

Open your any kind of text editor(notepadd++, etc..). Then just copy/paste the code below then name it "conn.php".

  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'db_print'

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Can't connect to database"

    )

    ;
  5. }
  6. ?>

Creating The Interface

This is where we will create the appearance of an application. To create this simply copy and write this block of code inside the text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - How To Print A Document</h3>
  16. <hr style="border-top:1px dotted #ccc;" />
  17. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"><span class="glyphicon glyphicon-plus"></span> Add Product</button>
  18. <br /><br />
  19. <a href="print.php" target="_blank" class="btn btn-success pull-right"><span class="glyphicon glyphicon-print"></span> Print</a>
  20. <br />
  21. <br />
  22. <table class="table table-bordered">
  23. <thead class="alert-success">
  24. <tr>
  25. <th>Product Name</th>
  26. <th>Price</th>
  27. <th>Qty</th>
  28. <th>Data Added</th>
  29. </tr>
  30. </thead>
  31. <tbody style="background-color:#fff;">
  32. <?php
  33. require

    'conn.php'

    ;

  34. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `product`"

    )

    ;
  35. while

    (

    $fetch

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {

  36. ?>

  37. <tr>
  38. <td><?php

    echo

    $fetch

    [

    'pname'

    ]

    ?>

    </td>
  39. <td><?php

    echo

    $fetch

    [

    'price'

    ]

    ?>

    </td>
  40. <td><?php

    echo

    $fetch

    [

    'quantity'

    ]

    ?>

    </td>
  41. <td><?php

    echo

    $fetch

    [

    'date_added'

    ]

    ?>

    </td>
  42. </tr>

  43. <?php
  44. }
  45. ?>
  46. </tbody>
  47. </table>
  48. </div>
  49. <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  50. <div class="modal-dialog" role="document">
  51. <form action="save_query.php" method="POST">
  52. <div class="modal-content">
  53. <div class="modal-body">
  54. <div class="col-md-2"></div>
  55. <div class="col-md-8">
  56. <div class="form-group">
  57. <label>Product Name</label>
  58. <input type="text" name="pname" class="form-control"/>
  59. </div>
  60. <div class="form-group">
  61. <label>Price</label>
  62. <input type="number" name="price" class="form-control"/>
  63. </div>
  64. <div class="form-group">
  65. <label>Qty</label>
  66. <input type="number" name="qty" class="form-control"/>
  67. </div>
  68. </div>
  69. </div>
  70. <div style="clear:both;"></div>
  71. <div class="modal-footer">
  72. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  73. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Submit</button>
  74. </div>
  75. </div>
  76. </form>
  77. </div>
  78. </div>
  79. </body>
  80. <script src="js/jquery-3.2.1.min.js"></script>
  81. <script src="js/bootstrap.js"></script>
  82. </html>

Creating PHP

This code contains the save query of the application. This code will store the inputs to the database server. To do that copy and write these block of codes inside your text editor and save it as "save_query.php".

  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'save'

    ]

    )

    )

    {
  4. $pname

    =

    $_POST

    [

    'pname'

    ]

    ;
  5. $price

    =

    $_POST

    [

    'price'

    ]

    ;
  6. $qty

    =

    $_POST

    [

    'qty'

    ]

    ;
  7. $date

    =

    date

    (

    "Y-m-d"

    ,

    strtotime

    (

    "+6 HOURS"

    )

    )

    ;

  8. $conn

    ->

    query

    (

    "INSERT INTO `product` VALUES('', '$pname

    ', '$price

    ', '$qty

    ', '$date

    ')"

    )

    or die

    (

    mysqli_errno

    (

    )

    )

    ;

  9. header

    (

    'location: index.php'

    )

    ;
  10. }
  11. ?>

Creating the Main Function

This code contains the main function of the application. This code will display the data that will be printed when the button is clicked. To do this all you have to do is copy and write the code inside the text editor and save it as print.php.

  1. <!DOCTYPE html>
  2. <?php
  3. require

    'conn.php'

    ;
  4. ?>
  5. <html lang="en">
  6. <head>
  7. <style>
  8. .table {
  9. width: 100%;
  10. margin-bottom: 20px;
  11. }

  12. .table-striped tbody > tr:nth-child(odd) > td,
  13. .table-striped tbody > tr:nth-child(odd) > th {
  14. background-color: #f9f9f9;
  15. }

  16. @media print{
  17. #print {
  18. display:none;
  19. }
  20. }
  21. @media print {
  22. #PrintButton {
  23. display: none;
  24. }
  25. }

  26. @page {
  27. size: auto; /* auto is the initial value */
  28. margin: 0; /* this affects the margin in the printer settings */
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <h2>Sourcecodester</h2>
  34. <br /> <br /> <br /> <br />
  35. <b style="color:blue;">Date Prepared:</b>
  36. <?php
  37. $date

    =

    date

    (

    "Y-m-d"

    ,

    strtotime

    (

    "+6 HOURS"

    )

    )

    ;
  38. echo

    $date

    ;
  39. ?>
  40. <br /><br />
  41. <table class="table table-striped">
  42. <thead>
  43. <tr>
  44. <th>Product Name</th>
  45. <th>Price</th>
  46. <th>Qty</th>
  47. <th>Data Added</th>
  48. </tr>
  49. </thead>
  50. <tbody>
  51. <?php
  52. require

    'conn.php'

    ;

  53. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `product`"

    )

    ;
  54. while

    (

    $fetch

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {

  55. ?>

  56. <tr>
  57. <td style="text-align:center;"><?php

    echo

    $fetch

    [

    'pname'

    ]

    ?>

    </td>
  58. <td style="text-align:center;"><?php

    echo

    $fetch

    [

    'price'

    ]

    ?>

    </td>
  59. <td style="text-align:center;"><?php

    echo

    $fetch

    [

    'quantity'

    ]

    ?>

    </td>
  60. <td style="text-align:center;"><?php

    echo

    $fetch

    [

    'date_added'

    ]

    ?>

    </td>
  61. </tr>

  62. <?php
  63. }
  64. ?>
  65. </tbody>
  66. </table>
  67. <center><button id="PrintButton" onclick="PrintPage()">Print</button></center>
  68. </body>
  69. <script type="text/javascript">
  70. function PrintPage() {
  71. window.print();
  72. }
  73. </script>
  74. </html>

Set printable table to Print View automatically

If you wanted to set the "print.php" into Print View automatically. Copy and paste the code below after the PrintPage() function inside the "script" tag. The code will open in a new window and automatically viewed in print view after the data loaded.

  1. window.addEventListener

    (

    'DOMContentLoaded'

    ,

    (

    event)

    =>

    {
  2. PrintPage(

    )
  3. setTimeout(

    function

    (

    )

    {

    window.close

    (

    )

    }

    ,

    750

    )
  4. }

    )

    ;

The javascript code above helps also to automatically close the print page after printing or cancel printing.

There you have it we already learn on How To Print Document using PHP. I hope that this simple tutorial helps you understand the difficulties of PHP. For more updates and tutorials just kindly visit this site.

Enjoy Coding!!!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,508

356,407

356,420

Top