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

Asked

Viewed 792 times

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>
  • There is no way to do this. If it were otherwise PHP -> Javascript would give, but JS -> PHP has no way.

  • What you can do is send this information via ajax.

  • @Wictorchaves and how do I do it? by chance I never worked with ajax... :s

  • @Wictorchaves got it, buddy! I have already implemented your code in my project but still n updates the session variable because imagining that I change page and back again the amount values are the same when the user entered the product in your cart and n the new values of Qta that it introduced before leaving. that is, still my problem...

  • Try debugging by pressing F12

1 answer

0

You can do it this way:

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

Here I am defining the shipping method, the url of the archive php where you will be sent via post the variable x.

Ajax documentation using jquery

How I used the method post 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 I’m picking it up post the variable x, which was defined in data in our ajax.

What the full code would look like:

<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>

Obs: by default when using the function ajax of jquery the option async this as true, that is, it will call the code php and will continue script processing even before the end of processing of php to finish, if you wish not to, if you want it to wait for the code to be completed php for continue the normal process of script, you must use the async as false, in this way:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.