Hamza Shah

Hamza Shah

  • NA
  • 87
  • 23k

Inserting data into two database tables through Table tbody

Oct 29 2020 12:29 AM
What I'm trying to do is when I select number of products from dropdown it appears in below table. Now when I click Save button below the table, I want data to be inserted in two Database table i.e Sales and SaleItems. Well Data is inserting in Sales table but I don't know how to insert in another table SaleItems. Please help me how can I do that?
 
Here's my code
  1. <table id="table" class="table">  
  2. <thead>  
  3. <tr>  
  4. <th scope="col">Product Id</th>  
  5. <th scope="col">Company</th>  
  6. <th scope="col">Product Name</th>  
  7. <th scope="col">User Name</th>  
  8. <th scope="col">User Mobile</th>  
  9. <th scope="col">Price per product</th>  
  10. <th scope="col">Quantity</th>  
  11. <th scope="col">Total Price</th>  
  12. </tr>  
  13. </thead>  
  14. <tbody></tbody>  
  15. <tfoot><tr><td colspan="2">Grand Total : </td><td id="GrandTotal"></td></tr></tfoot>  
  16. </table>  
  17. <script>  
  18. $('#productSelect').change(function () {  
  19. var id = $(this).val();  
  20. if (id > 0) {  
  21. $.get("GetProduct", { productId: id }, function (result) {  
  22. console.log(result)  
  23. $("tbody").append("<tr><td>" + result.ProductId + "</td><td>" + result.CompanyName + "</td><td>" + result.ProductName + "</td><td>" + result.UserName + "</td><td>" + result.UserMobile + "</td><td>" + result.ProductPrice + "</td><td><button type='button' class='btn btn-primary' onClick='subtract(" + id + "," + result.ProductPrice + ")'>-</button><button type='button' class='btn btn-dark' id='" + id + "' value='0'>0</button><button type='button' class='btn btn-primary' onClick='add(" + id + "," + result.ProductPrice + ")'>+</button></td><td id='sum" + id + "'>0</td><td><a onclick='removeRow(this)'>x</a></td></tr>")  
  24. CalculateGrandTotal();  
  25. });  
  26. }  
  27. })  
  28. $("body").on("click""#btnSave"function () {  
  29. //Loop through the Table rows and build a JSON array.  
  30. var sales = new Array();  
  31. $(".table tbody tr").each(function () {  
  32. var row = $(this);  
  33. var Sale = {};  
  34. Sale.UserName = row.find("td").eq(3).html();  
  35. Sale.UserMobile = row.find("td").eq(4).html();  
  36. Sale.NetTotal = row.find("td").eq(8).html();  
  37. Sale.ProductQuantity = row.find("td").eq(5).val();  
  38. sales.push(Sale);  
  39. });  
  40. </script>  

Answers (3)