Guest User

Untitled

a guest
Jun 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. PHP processing - trolley1.php
  2. <?php
  3. session_start();
  4. $product_ids = array();
  5.  
  6. //check if Add to Cart button has been submitted
  7. if(filter_input(INPUT_POST, 'submit')){
  8. if(isset($_SESSION['cart'])){
  9.  
  10. //keep track of how many products are in the cart
  11. $count = count($_SESSION['cart']);
  12.  
  13. //create array for matching array keys to products id's
  14. $product_ids = array_column($_SESSION['cart'], 'id');
  15.  
  16. if (!in_array(filter_input(INPUT_GET, 'id'), $product_ids)){
  17. $_SESSION['cart'][$count] = array(
  18. 'id' => filter_input(INPUT_GET, 'id'),
  19. 'name' => filter_input(INPUT_POST, 'name'),
  20. 'price' => filter_input(INPUT_POST, 'price'),
  21. 'quantity' => filter_input(INPUT_POST, 'quantity')
  22. );
  23. }
  24. else { //product already exists, increase quantity
  25. //match array key to id of the product being added to the cart
  26. for ($i = 0; $i < count($product_ids); $i++){
  27. if ($product_ids[$i] == filter_input(INPUT_GET, 'id')){
  28. //add item quantity to the existing product in the array
  29. $_SESSION['cart'][$i]['quantity'] += filter_input(INPUT_POST, 'quantity');
  30. }
  31. }
  32. }
  33. }
  34. else { //if shopping cart doesn't exist, create first product with array key 0
  35. //create array using submitted form data, start from key 0 and fill it with values
  36. $_SESSION['cart'][0] = array(
  37. 'id' => filter_input(INPUT_GET, 'id'),
  38. 'name' => filter_input(INPUT_POST, 'name'),
  39. 'price' => filter_input(INPUT_POST, 'price'),
  40. 'quantity' => filter_input(INPUT_POST, 'quantity')
  41. );
  42. }
  43. }
  44.  
  45. cart.php
  46. <script>
  47. $(document).ready(function(){
  48. $('form[id=ajax]').submit(function(event){
  49. event.preventDefault();
  50.  
  51. var formData = {
  52. submit: $('input[name=submit]').val(),
  53. id: $('input[name=id]').val(),
  54. name: $('input[name=name]').val(),
  55. price: $('input[name=price]').val(),
  56. quantity: $('input[name=quantity]').val()
  57. };
  58.  
  59. $.ajax({
  60. type: 'POST',
  61. url: 'trolley1.php',
  62. data: formData,
  63. dataType: 'json',
  64. });
  65. });
  66. });
  67. </script>
  68. <?php
  69. echo "
  70. <div class='col-sm-3 mt-5'>
  71. <form id='ajax' method='post' action='trolley1.php?action=add&id=$id'>
  72. <div class='products'>
  73. <input id='form_id' type='hidden' name='id' value='$id' />
  74. <input id='form_name' type='hidden' name='name' value='$product' />
  75. <input id='form_price' type='hidden' name='price' value='$price' />
  76. <input id='form_quantity' type='text' name='quantity' class='form-control' value='1' />
  77. <input id='submit' type='submit' name='submit' style='margin-top:5px;' class='btn btn-info'
  78. value='Add'/>
  79. <div id='get_content'></div>
  80. </div>
  81. </form>
  82. </div>
Add Comment
Please, Sign In to add comment