title: ajax add to cart using php and sessions
body: <p>Similarly as in <a href="https://stackoverflow.com/questions/47482086/php-ajax-add-to-cart-not-working">PHP Ajax add to cart not working</a> my add to cart is also not working. I am trying to show the added product name, price, quantity and total price in                                        <code>&lt;div id="mycart"&gt; here &lt;/div&gt;</code> but it's not working and I am not getting any errors in console. Things that do work:</p>
  
  <ul>
  <li>I get the success alert: Product has been added to cart
  but it doesn't show up in my div. </li>
  </ul>
  
  <p>Can anyone tell me what I am doing wrong?</p>
  
  <p>My form on products.php</p>
  
  <pre><code>&lt;form class="form-item" method="post" action="?action=add&amp;id=&lt;?php echo $row["id"]; ?&gt;"&gt; 
  bla bla input
  &lt;input type="submit" name="add_to_cart" class="btn btn-outline-secondary btn-sm" value="Add to cart"&gt;
  </code></pre>
  
  <p>My ajax.js</p>
  
  <pre><code>&lt;script&gt;
  $('.form-item').on('submit', function() {
  
      var id = $(this).attr("id");
      var titel = $('#titel' + id).val();
      var price = $('#price' + id).val();
      var quantity = $('#quantity' + id).val();
  
  $.ajax({
             url:"cart-process.php",
             method:"POST",
             dataType:"json",
             data:{
                id:id,
                titel:titel,
                price:price,
                quantity:quantity,
              },
              success:function(data)
              {
               $('#mycart').html(data);
               alert("Product has been added to cart");
              }
          });
      return false;
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>and my cart-process.php</p>
  
  <pre><code>&lt;?php   
  session_start();
  
  if(!empty($_SESSION['shopping_cart'])){  
      $total = 0;  
  
      foreach($_SESSION['shopping_cart'] as $key =&gt; $row){ 
  
      $output .='&lt;a href="?action=delete&amp;id='.$row['id'].'" class="action-icon close-shopping-cart-box"&gt;&lt;i class="ti ti-close"&gt;&lt;/i&gt;&lt;/a&gt;';
      $output .='&lt;span&gt;'.$row['titel'].'&lt;/span&gt;&lt;span class="text-muted"&gt; x '.$row['quantity'].'&lt;/span&gt;&lt;br /&gt; ';
  
  
      $total = $total + ($row['quantity'] * $row['prijs']);  
      }  
  
      $output.='&lt;br /&gt;Totaal:&amp;euro; '.number_format($total, 2).'';
  
  
  
  if (isset($_SESSION['shopping_cart'])){
  if (count($_SESSION['shopping_cart']) &gt; 0){
  
      $output.='&lt;a href="checkout-cash" style="margin-top:20px;" class="btn btn-outline-warning"&gt;Cash payment&lt;/a&gt;';
      $output.='&lt;a href="checkout-paypal" style="margin-top:20px;" class="btn btn-outline-warning"&gt;Paypal payment&lt;/a&gt;';
  
   }}
  </code></pre>
  
  <p>} </p>
  
  <pre><code>echo json_encode($output);
  ?&gt;
  </code></pre>