php - Symfony2 is it possible to remove session variable with Javascript? -
when displaying selected products saved in sessions have button want remove specific product if dont need it. can achieve javascript? if not other solutions problem?
i have heard cant set session variables javascript same goes removing them, have heard can ajax remove them?? anyway im displaying products this(for im showing price of product dynamically):
{% item in items %} <tr> <td><img width="60" src="{{ asset('bundles/mpfrontend/assets/products/4.jpg') }}" alt=""/></td> <td>{{ item.model }}</td> <td> <div class="input-append"><input class="span1" style="max-width:34px" placeholder="1" id="appendedinputbuttons" size="16" type="text"> <button class="btn" type="button"><i class="icon-minus"></i></button> <button class="btn" type="button"><i class="icon-plus"></i></button> <button class="btn btn-danger" type="button" onclick="removeitem(item.id)"><i class="icon-remove icon-white"></i></button> </div> </td> <td>$120.00</td> <td>$25.00</td> <td>$15.00</td> <td>$110.00</td> </tr> {% endfor %}
update have done already:
removeaction in controller:
public function removeaction($itemid) { $session = $this->getrequest()->getsession(); $session->remove(); return $this->render('mpshopbundle:frontend:product_summary.html.twig'); }
controller routing:
removeitem: pattern: /remove defaults: { _controller: mpshopbundle:homepage:remove }
the script:
<script> $(".btn btn-danger").click(function(){ var itemid = $(this).val(); $.ajax({ type: "post", url: "{{ path('removeitem') }}", data: { itemid: itemid } }); </script>
pressing on button doesnt , im not surprised since first time using javascript guess did wrong?
yes can ajax !
- create action in controller remove given product of session.
a sample code :
productcontroller extends controller{ ... public function removeitemaction($itemid){ //find here session save item. //and remove //return response depending on want in format want (json,xml,...) return new response("..."); } }
2.create javascript code listen action , send request ajax precedent url. (if use jquery , see $.ajax )
- update dom (with deleting right elements or load html want ).