0
This is my shopping cart:
<?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>';
}
?>
This is 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 I can assign the variable x
to this session $_SESSION["shopping_cart"][$k1]["item_quantity"]
?
var x = "<? php echo $_SESSION["shopping_cart"][$K1]["item_quantity"]; ? >";
– Willian Coqueiro
good! I tried to do what you said but do not give... with this instruction the price does not even update taking into account the quantity
– Tiago Simoes
Php will only run once and give you the results. What you have in the variable at that time and what you will write.
– Willian Coqueiro
Reread here. It seems that you want the value of the Javascript variable x to pass to the php session. And this?
– Willian Coqueiro
@Williancoqueiro yes that’s what I want to do. I want to pass the value of variable x to the php session
– Tiago Simoes
You will not get it that way. Javascript and Client-side, must send a request for php to save the session. Or by ajax or a post, get request.
– Willian Coqueiro
@Williancoqueiro never worked with ajax... could give there a help to see more or less how it would look?
– Tiago Simoes