Search code examples
phpjquerycomboboxsmarty

Adding a Smarty variable with total value of the sum of three combo box


I have this code for add (sume up) three combo values and I want to sume up the val "precioinicial" from smarty variable with the result (resultado) But I don't know how to do it. Any help? Thank you very much.

$(document).ready(function(e) {
        $('#form1').change(function() {
           var obj = {};
           var string = '';
           var precioinicial  = "{$productes_precio}";
           var sum  = 0;
           // Grupo 1
           if ($("select option[name=embroider]").is(':checked')) {
              var n1 = $("select option[name=embroider]:checked").val();
              sum += parseFloat(n1);
           }
            // Grupo 2
           if ($("select option[name=sole]").is(':checked')) {
              var n1 = $("select option[name=sole]:checked").val();
              sum  += parseFloat(n1);
           }
          // Grupo 3
           if ($("select option[name=taps]").is(':checked')) {
              var n1 = $("select option[name=taps]:checked").val();
              sum += parseFloat(n1);
           }
           $('#resultado').val(sum);
        });
});

Solution

  • You don't specify whether the code is in a .js file or in a template processed by smarty. If it's the first case, you should at least declare precioinicial in some place of the template file (probably the head), i.e.

    <html>
      <head>
      ...
        <script>
           var precioinicial  = "{$productes_precio}";
         </script>
      </head>
      <body>
      ...
    

    then in you script you just have to use the variable and assign it to sum:

    $('#form1').change(function() {
               var obj = {};
               var string = '';
               var sum  = parseFloat(precioinicial);
    

    If the rest of your code is correct, it should work as expected.