title: Updating to database using ajax/jQuery
body: <p>I’m having trouble using ajax to make an update to my database. Basically I have the jQuery number spinner and when the user clicks up that number for that product is meant to be uploaded to the database. Instead either all the products are updated to that number or the last one is. How can I make it so only the product the user is changing be changed in the database?</p>
  
  <p>It’s worth noting that I’m a total noob at jQuery/ajax but I’m trying to learn.</p>
  
  <p>Twig/HTML:</p>
  
  <pre><code>{% extends '::base.html.twig' %}
  
  {% block body %}
  
  &lt;h1 class="text-center"&gt;&lt;u&gt;&lt;i&gt;Your Cart&lt;/i&gt;&lt;/u&gt;&lt;/h1&gt;
  
      &lt;div class="container"&gt;
  
          &lt;div class="who_is_logged_in"&gt;
              {% if user is null %}
                  &lt;a href="{{ path ('fos_user_security_login') }}"&gt;Login&lt;/a&gt;
              {% else %}
                  &lt;u&gt;Hello&lt;strong&gt; {{ user }}&lt;/strong&gt;&lt;/u&gt;
              {% endif %}
          &lt;/div&gt;
  
          &lt;table class="table table-striped"&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Product&lt;/th&gt;
                      &lt;th&gt;Quantity&lt;/th&gt;
                      &lt;th&gt;Price Per Unit&lt;/th&gt;    
                      &lt;th&gt;Remove Product&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;
  
              &lt;tbody&gt;
              {% for key, product in quantity %}  
                      &lt;tr&gt;
                          &lt;td&gt;{{ product.product }}&lt;/td&gt; &lt;!--Product--&gt;
                          &lt;td&gt;
                              &lt;input class="spinner" value="{{ product.quantity }}" style="width:30px"&gt;
                          &lt;/td&gt; &lt;!--Quantity--&gt;
                          &lt;td&gt;${{ product.product.price|default('') }}&lt;/td&gt; &lt;!--Price--&gt;   
                          &lt;td&gt;
                              &lt;a href="{{ path('product_remove', {'id': product.product.id }) }}"&gt;
                                  &lt;button name="REMOVE" type="button" class="btn btn-danger" id="removeButton"&gt;
                                      &lt;span class="glyphicon glyphicon-remove" aria-hidden="true"&gt;&lt;/span&gt;
                                  &lt;/button&gt;
                              &lt;/a&gt;
                          &lt;/td&gt;&lt;!--Remove--&gt; 
                      &lt;/tr&gt;
                  {% endfor %}      
              &lt;/tbody&gt;
          &lt;/table&gt; &lt;!--top table--&gt;
  
              &lt;div class="money-container"&gt;
                  &lt;p class="text-right"&gt;Total Cost: ${{ totalCostOfAllProducts }}&lt;/p&gt;
              &lt;/div&gt; &lt;!--moneyContainer--&gt;
  
          {% for flash_message in app.session.flashbag.get('notice') %}
              &lt;div class="flash-notice"&gt;
                  &lt;strong&gt;{{ flash_message }}&lt;/strong&gt;
              &lt;/div&gt; &lt;!--flashNotice--&gt;
          {% endfor %}
  
      &lt;/div&gt; &lt;!--container--&gt;
  
      &lt;ul&gt;
          &lt;li&gt;
              &lt;a href="{{ path('product') }}"&gt;
                  Add More Products
              &lt;/a&gt;
          &lt;/li&gt;
          &lt;li&gt;
              &lt;a href="{{ path('product_bought') }}"&gt;
                  Buy These Products
              &lt;/a&gt;
          &lt;/li&gt;
      &lt;/ul&gt;
  
  
      &lt;script type="text/javascript"&gt;
       $(".spinner").spinner();
          $('input.spinner').on('spinstop', function(){
              min: 0
              console.log('spinner changed');
              var $this = $(this);
              var value = $('.spinner').val();
              var request = $.ajax({
                  url: "{{ path('product_quantityUpdate') }}",
                  method: "POST",
                  data : {
                      quantity: this.value,
                  }
              }).done(function(result){
                  console.log('success', result);
              }).error(function (xhr, ajaxOptions, thrownError) {
                  console.log(xhr.status);
                  console.log(xhr.responseText);
                  console.log(thrownError);
              });
          });
      &lt;/script&gt;
  
  {% endblock %}
  </code></pre>
  
  <p>Relavent Controller Functions:</p>
  
  <pre><code>/**
   * Updates quantity using ajax/jQuery request from showCart twig file
   *
   * @Route("/quantityUpdate", name="product_quantityUpdate")
   * @Method("POST")
   * @Template()
   */
  public function quantityUpdateAction(Request $request) {
  
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $cart = $em-&gt;getRepository('ShopBundle:UserCart')-&gt;findOneBy(['user' =&gt; $this-&gt;getUser(), 'submitted' =&gt; false]);
  
      $productsInUsersCart = $cart-&gt;getQuantities();
  
      foreach ($productsInUsersCart as $key =&gt; $value) {
  
          $productsInUsersCart = $value-&gt;getQuantity();   // Within the loop, gets the quantity of the product being changed
              $value-&gt;getProduct()-&gt;getId();     //SHOULD be the product ID
              $value-&gt;getUserCart()-&gt;getId();   //SHOULD be the user's cart ID
  
  /* ----------------------------- Like Below &amp; Will Update EVERYTHING in Users Cart ----------------------------- */
  
      // dump($request-&gt;get('id_value'));
      // dump($request-&gt;get('prodId')); die;
          $value-&gt;setQuantity($_POST['quantity']);
          $em-&gt;persist($value);
          $em-&gt;flush();
      }
  
  /* ----------------------------- Like Below &amp; Will Update the LAST product in Users Cart ----------------------------- */
  
          // $value-&gt;setQuantity($_POST['quantity']);
          // $em-&gt;persist($value);
          // $em-&gt;flush();
  
      return new Response ("This has been successful!");
  }
  </code></pre>