将输入值作为会话变量传递

passing input value as session variable

我有一个名为 qty 的输入字段,必须将其值传递到下一个名为 update_cart 的页面进行处理。我尝试使用会话变量。但是当我尝试通过 $_Post$_Get 发送它时,它无法按预期工作。下面给出了我尝试过的任何建议 please.The 代码:

cart.php

echo "<table class='table table-hover table-responsive table-bordered'>";
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price (USD)</th>";
echo "<th>Quantity</th>";
echo "<th>Action</th>";
echo "<th>Updated price</th>";
echo "</tr>";
$query = "SELECT * FROM sub_products WHERE sub_p_id IN ({$ids}) ORDER BY name";
$stmt = $con->prepare( $query );
$stmt->execute();

$total_price=0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
echo "<tr>";
echo "<td>".$row['name']."</td>";
echo "<td>&#36;".$row['price']."</td>";
echo "<td>";
echo "<input type='number' name='qty' value='' max='10'>";
$_SESSION['qty']=2 ; //here i want to pass value of "qty"
echo "<a href='update_cart.php?id={$id}&name=".$row['name']."&price=".$row['price']."&qty=".$_SESSION['qty']."' class='btn btn-danger'>";
 echo "<span </span> Update Price </a>";
 $total_price+=$price;
  }
echo "<tr>";
echo "<td><b>Total</b></td>";
echo "<td>&#36;{$total_price}</td>";

update_cart.php

session_start();
$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$qty=$_SESSION['qty'];
$price=isset($_GET['price'])? $_GET['price']: "";
$price=$price*$qty;
echo $qty;
header('Location: cart.php?action=quantity_updated&id=' . $id . '&name=' . $name . '&price='.$price . '&qty='.$qty);

您首先需要从 GET 变量中获取数量。

$qty = isset($_GET['qty']) ? $_GET['qty'] : 1;
$_SESSION['qty'] = $qty;

或简化:

$_SESSION['qty'] = isset($_GET['qty']) ? $_GET['qty'] : 1;

您不能在不提交表单的情况下向变量添加值,因为 PHP 是一种服务器端语言。页面一显示,PHP 就完成了,无法更改。

更新

为了让您更轻松一些,我将向您展示如何使用表单。

cart.php

echo "<table class='table table-hover table-responsive table-bordered'>";
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price (USD)</th>";
echo "<th>Quantity</th>";
echo "<th>Action</th>";
echo "<th>Updated price</th>";
echo "</tr>";
$query = "SELECT * FROM sub_products WHERE sub_p_id IN ({$ids}) ORDER BY name";
$stmt = $con->prepare( $query );
$stmt->execute();

$total_price=0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
?>
<tr>
    <form method="POST" action="update_cart.php">
    <td><?= $row['name']; ?></td>
    <td>&#36; <?= $row['price']; ?></td>
    <td>
        <input type='number' name='qty' value='' max='10'>
    </td>
    <td>
        <input type="submit" name="submit" value="Submit">
    </td>
    <input type="hidden" name="id" value="<?= $row['id']; ?>" />
    <input type="hidden" name="name" value="<?= $row['name']; ?>" />
    <input type="hidden" name="price" value="<?= $row['price']; ?>" />
    </form>
</tr>

更新_cart.php

变化:

$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$qty=$_SESSION['qty'];
$price=isset($_GET['price'])? $_GET['price']: "";
$price=$price*$qty;

至:

$id = isset($_POST['id']) ? $_POST['id'] : "";
$name = isset($_POST['name']) ? $_POST['name'] : "";
$qty=$_SESSION['qty'];
$price=isset($_POST['price'])? $_POST['price']: "";
$price=$price*$qty;