How do I get the value of a variable declared in javascript and assign it to a value of a session (using an array) in php?

0

My shopping cart is this:

<?php
    $c = count($_SESSION["shopping_cart"]);
    echo '<script>console.log("termina com contador = ' . $c . '")</script>';
    for ($i = 0; $i < $c; $i++) {
        echo '<script>console.log("' .
        $_SESSION["shopping_cart"][$i]["item_id"] . ' | ' .
        $_SESSION["shopping_cart"][$i]["item_name"] . ' | ' .
        $_SESSION["shopping_cart"][$i]["item_price"] . ' | ' .
        $_SESSION["shopping_cart"][$i]["item_quantity"] .
        '")</script>';
    }
?>  

Here's how all the shopping cart details are listed:

//----------------------------------------------------
<?php
require_once 'liga.php';

$a = 0;
//----------------------------------------------------
foreach ($_SESSION["shopping_cart"] as $keys => $values) {
    $max_produto = 0;
    $con = ligabd();
    $query = $con->query("select Stock, price from tbl_product where id =" . $values["item_id"]);
    while ($registos = $query->fetch_array()) {
        $max_produto = $registos[0]; //query p receber o max de stock
        $preco = $registos[1];
    }
    $a++;
    ?>

    <tr> 
        <td><?php echo $values["item_name"]; ?></td>
        <td><input type="number" id="<?php echo "p" . $a; ?>" min="1" max="<?php echo $max_produto; ?>" value="<?php echo $values["item_quantity"]; ?>"></td>
        <!--<td><input type="text" id="<?php // echo "txt_p" . $a;   ?>" value="<?php // echo $values["item_price"] * $values["item_quantity"];   ?>">€</td>-->
        <td><label id="<?php echo "txt_p" . $a; ?>"><?php echo $values["item_price"] * $values["item_quantity"]; ?>€</label></td>
        <td><a style="text-decoration: none;" href="ver_detalhes.php?action=delete&id=<?php echo $values["item_id"]; ?>"><span class="text-danger">X</span></a></td>
        <td><input type="text" id="<?php echo "preco" . $a; ?>" value="<?php echo $preco; ?>"></td>
        <!--<td><label id="<?php // echo "teste" . $a;  ?>"><?php // echo $values["item_quantity"];  ?></label></td>-->
        <td><input type="text" id="<?php echo "teste" . $a; ?>" value="<?php echo $values["item_quantity"]; ?>"></td>
    </tr>

This is my JavaScript

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


                alert("Qta produto ------ " + x);

                <?php
//                 $c = count($_SESSION["shopping_cart"]);
//                 for ($k1 = 0; $k1 < $c; $k1++) {
//                    $_SESSION["shopping_cart"][$k1]["item_quantity"] = **x**;
//                 }
                ?>
                }
            });
        });
    </script>

My question is how can I assign the x variable to this session $_SESSION["shopping_cart"][$k1]["item_quantity"] ?

    
asked by anonymous 18.05.2018 / 23:08

0 answers