2

I'm work with symfony for an E-commerce project. I have like to display a pop-up window in relation to each product. this pop-up window have a list choices. so for that I like to pass the controller for this window by Ajax to be dynamic , for that :

Code HTML

  {% for  p in products %}
     <a id="#basket-modal" href="#" data-id="{{ p.id }}" data-toggle="modal" data-target="{{ modal }}" class="btn btn-primary">
  {% endfor %}

Script

<script type="text/javascript">
 $(document).ready(function(){

  $("#basket-modal").on({ click: function() {


   var op_prod_id = $(this).data('id');

   $.ajax({

       url: "{{ path('ajout_prod_panier', {'id': op_prod_id }) }}" , 
       type: "POST",
       data: "op_prod_id="+op_prod_id,
       success: function(data, status, xhr) {
         console.log(data); 
       },
   error: function(jqxhr, status, error) {
     console.log(error);
       }
   });
   event.stopPropagation();
 }
});
});
</script>

the problem that always I have an error message :

Variable "op_prod_id" does not exist.

in url:

"{{ path('ajout_prod_panier', {'id': op_prod_id }) }}"

1 Answer 1

2

var op_prod_id = $(this).data('id'); is a js variable.

{{ path('ajout_prod_panier', {'id': op_prod_id }) }} is a twig expression.

Try something like that:

 <a href="#" data-id="{{ p.id }}" data-url={{ path('ajout_prod_panier', {'id': p.id}) }}"  data-toggle="modal" data-target="{{ modal }}" class="basket-modal btn btn-primary">

and in your js

$(".basket-modal").on({ click: function() {

    var op_prod_id = $(this).data('id');
    var op_prod_url = $(this).data('url');
    ....
    url: op_prod_url
Sign up to request clarification or add additional context in comments.

11 Comments

thanks @Doydoy44 it can be solution : but I have an other error in "$(document).ready(function()" => $ is not defined
@DhiaEddineFarah: This error means that your jquery file is not load before your js code.
I added " <script src="{{ asset ('template/js/jquery-1.11.0.min.js') }}"></script> " before the script, this error does not display, but the pop-up window does not display
@DhiaEddineFarah: I guess it's the same type of error, so try do put your js code after all js plugins, and use your js console log to debug. Good luck. :)
I do all that you tell me , but the pop-up still does not display :/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.