How to assign the value of a variable in javascript, within php?

0
<script>
  $(document).ready(function () {
    $("input").change(function () {
      var carrinho = $("#carrinho").val();
      //alert("Carrinho " + carrinho);
      var i;
      var soma = 0;
      for (i = 1; i <= carrinho; i++) {
        //alert(i);
        var x = $("#p" + i).val();
        //alert(i+"---- "+x);

        var y = $("#txt_p" + i).val();
        //alert(i+ "------ "+y);

        var z = $("#preco" + i).val();
        //alert(i + "------ "+z);
        var total = x * z;
        //alert("Total = "+ total);
        var atualiza = total;
        //alert("Valor atualizado = "+ total);
        soma = soma + total;
        $("#txt_p" + i).text(atualiza + "€");
        $("#total").text(soma + "€");
        $("#teste" + i).val(x);


        //a variavel x irá armazenar a quatidade atual do produto

          <?php
              $c = count($_SESSION["shopping_cart"]);
              for ($k1 = 0; $k1 < $c; $k1++) {
                  $_SESSION["shopping_cart"][$k1]["item_quantity"] = x;
              }
          ?>
         }
        });
      });
    </script>
    
asked by anonymous 19.05.2018 / 21:22

1 answer

0

You can do it this way:

$.ajax({
  method: "POST",
  url: "action.php",
  data: { x: x }
});

Here I am defining the sending method, the url of the file php in which the post variable will be sent via x .

Ajax documentation using jquery

As I used the method post I made the file action.php as follows:

<?php
$c = count($_SESSION["shopping_cart"]);
for ($k1 = 0; $k1 < $c; $k1++) {
    $_SESSION["shopping_cart"][$k1]["item_quantity"] = $_POST['x'];
}

Notice that I'm getting via%% of the variable post , which was set to x in our data .

What would be the complete code:

<script>
$(document).ready(function () {
    $("input").change(function () {
        var carrinho = $("#carrinho").val();
        //alert("Carrinho " + carrinho);
        var i;
        var soma = 0;
        for (i = 1; i <= carrinho; i++) {
        //alert(i);
        var x = $("#p" + i).val();
        //alert(i+"---- "+x);

        var y = $("#txt_p" + i).val();
        //alert(i+ "------ "+y);

        var z = $("#preco" + i).val();
        //alert(i + "------ "+z);
        var total = x * z;
        //alert("Total = "+ total);
        var atualiza = total;
        //alert("Valor atualizado = "+ total);
        soma = soma + total;
        $("#txt_p" + i).text(atualiza + "€");
        $("#total").text(soma + "€");
        $("#teste" + i).val(x);

        $.ajax({
          method: "POST",
          url: "some.php",
          data: { x: x }
        });

        }
    });
});
</script>
  

Note: by default when using the ajax function of ajax option    jquery is like async , that is, it will call the code true and will   continue processing the script even before the end of the    php processing, if you wish not to do so,   if you want it to wait for the completion of the code php to   To continue the normal php process, you should use script as    async , like this:

$.ajax({
  method: "POST",
  url: "some.php",
  async: false,
  data: { x: x }
});
    
20.05.2018 / 23:19