Create Dynamic Invoice Form Using PHP

Introduction:

Today, I am starting to write an article about dynamic invoice forms in PHP because we need this type of form in a professional way and it is very beneficial for project study because we cant know the actual rows.

Step 1:

First we open the localhost and click on phpmyadmin on the left side, then make two tables names as tbl_orderdetail and tbl_order, but remember to make a primary key in both tables and check the AUTO INCREMENT box.You see the structure of both tables here.

TABLE NAME: tbl_orderdetail

TABLE

TABLE NAME: tbl_order


TABLE

Step 2:

Then we add some online libraries of JavaScript, jQuery and bootstrap in it, and if you want work offline you also download the libraries and make an invoice folder and make three sub folders and put all three libraries in it and then add in the main form, which is known as index.php, which means make a file in notepad++ and save as .php extension. Here you can see the code, I added online libraries as well as offline libraries.

code

Step 3:

Now we design the receipt form by declaring the two input and labels tags in the form and one button and put some bootstrap classes to make a unique form like this,

form

Here is the code you can see that we declare the form tag and then you see that in div tag we declare the class in it; this class is basically the bootstrap class.

code

Step 4:

Now we design invoice table, and make a table. It is mandatory that you have knowledge about how to create a table in HTML. Then after creating a table we add some bootstrap classes to make a table unique.

table

Here is the code,

code

Step 5:

Now we add JavaScript function because you can see that in table on right side the plus button is appearing; the function of this button is that when you press this button automatically a row is added because we designed it dynamically. If user need two rows then they add two rows so we give the choice to the user, so for this approach we make JavaScript function,

table

Here is the code,

code

Step 6:

Now inthe  previous step you can see that the total sum is calculated, and we also declare some formulas but discount columns. You can better understand when you see the coding.

code

Step 7:

Now we make a connection in this file but it’s not good practice.  You can make it separate; it is better and good practice. In this setup we put two insert queries to save the information of invoice tables and you also know that the rows are undefined because it depends upon the user, so we put loop to count the rows and then put it in database. Here is the code you can understand better.

code

Step 8:

Now I share the full source code to understand better.

  1. <?php  
  2. $cn=mysql_connect('localhost','root','');  
  3. if ($cn)  
  4. {  
  5. mysql_select_db('inv',$cn);  
  6. }  
  7. if(isset($_POST['save']))  
  8. {  
  9. $name=$_POST['name'];  
  10. $location=$_POST['location'];  
  11. mysql_query("insert into tbl_order(name,location) VALUES ('$name','$location')");  
  12. $id=mysql_insert_id();  
  13. for($i = 0; $i<count($_POST['productname']); $i++)  
  14. {  
  15. mysql_query("INSERT INTO tbl_orderdetail  
  16. SET   
  17. order_id = '{$id}',  
  18. product_name = '{$_POST['productname'][$i]}',  
  19. quantity = '{$_POST['quantity'][$i]}',  
  20. price = '{$_POST['price'][$i]}',  
  21. discount = '{$_POST['discount'][$i]}',  
  22. amount = '{$_POST['amount'][$i]}'");   
  23. }  
  24. }   
  25. ?>  
  26.     <!DOCTYPE html>  
  27.     <html>  
  28.   
  29.     <head>  
  30.         <title>Invoice</title>  
  31.     </head>  
  32.     <scriptsrc="//code.jquery.com/jquery-1.12.0.min.js">  
  33.         </script>  
  34.         <scriptsrc="//code.jquery.com/jquery-migrate-1.2.1.min.js">  
  35.             </script>  
  36.             <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">  
  37.             <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
  38.             <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
  39.   
  40.             <body>  
  41.                 <form action="" method="POST">  
  42.                     <div class="box box-primary">  
  43.                         <div class="box-header">  
  44.                             <h3 class="box-title">Invoice </h3>  
  45.                         </div>  
  46.                         <div class="box-body">  
  47.                             <div class="form-group">  
  48.                                 ReceiptName  
  49.                                 <input type="text" name="name" class="form-control">  
  50.                             </div>  
  51.                             <div class="form-group">  
  52.                                 Location  
  53.                                 <input type="text" name="location" class="form-control">  
  54.                             </div>  
  55.                         </div>  
  56.                         <input type="submit" class="btnbtn-primary" name="save" value="Save Record">  
  57.                     </div><br/>  
  58.                     <table class="table table-bordered table-hover">  
  59.                         <thead>  
  60.                             <th>No</th>  
  61.                             <th>Product Name</th>  
  62.                             <th>Quantity</th>  
  63.                             <th>Price</th>  
  64.                             <th>Discount</th>  
  65.                             <th>Amount</th>  
  66.                             <th><input type="button" value="+" id="add" class="btnbtn-primary"></th>  
  67.                         </thead>  
  68.                         <tbody class="detail">  
  69.                             <tr>  
  70.                                 <td class="no">1</td>  
  71.                                 <td><input type="text" class="form-control productname" name="productname[]"></td>  
  72.                                 <td><input type="text" class="form-control quantity" name="quantity[]"></td>  
  73.                                 <td><input type="text" class="form-control price" name="price[]"></td>  
  74.                                 <td><input type="text" class="form-control discount" name="discount[]"></td>  
  75.                                 <td><input type="text" class="form-control amount" name="amount[]"></td>  
  76.                                 <td><a href="#" class="remove">Delete</td>  
  77. </tr>  
  78. </tbody>  
  79. <tfoot>  
  80. <th></th>  
  81. <th></th>  
  82. <th></th>  
  83. <th></th>  
  84. <th></th>  
  85. <th style="text-align:center;" class="total">0<b></b></th>  
  86. </tfoot>  
  87.   
  88. </table>  
  89. </form>  
  90. </body>  
  91. </html>  
  92. <script type="text/javascript">  
  93. $(function()  
  94. {  
  95. $('#add').click(function()  
  96. {  
  97. addnewrow();  
  98. });  
  99. $('body').delegate('.remove','click',function()  
  100. {  
  101. $(this).parent().parent().remove();  
  102. });  
  103. $('body').delegate('.quantity,.price,.discount','keyup',function()  
  104. {  
  105. vartr=$(this).parent().parent();  
  106. varqty=tr.find('.quantity').val();  
  107. var price=tr.find('.price').val();  
  108.   
  109. var dis=tr.find('.discount').val();  
  110. varamt =(qty * price)-(qty * price *dis)/100;  
  111. tr.find('.amount').val(amt);  
  112. total();  
  113. });  
  114. });  
  115. function total()  
  116. {  
  117. var t=0;  
  118. $('.amount').each(function(i,e)   
  119. {  
  120. varamt =$(this).val()-0;  
  121. t+=amt;  
  122. });  
  123. $('.total').html(t);  
  124. }  
  125. functionaddnewrow()   
  126. {  
  127. var n=($('.detail tr').length-0)+1;  
  128. vartr = '<tr>'+  
  129. '<td class="no">'+n+'</td>'+  
  130. '<td><input type="text" class="form-control productname" name="productname[]"></td>'+  
  131. '<td><input type="text" class="form-control quantity" name="quantity[]"></td>'+  
  132. '<td><input type="text" class="form-control price" name="price[]"></td>'+  
  133. '<td><input type="text" class="form-control discount" name="discount[]"></td>'+  
  134. '<td><input type="text" class="form-control amount" name="amount[]"></td>'+  
  135. '<td><a href="#" class="remove">Delete</td>'+  
  136. '</tr>';  
  137. $('.detail').append(tr);   
  138. }  
  139. </script>  

Read more articles on PHP:

Up Next
    Ebook Download
    View all
    Learn
    View all