如何将表单与表格集成?

时间:2014-04-18 19:15:20

标签: php

这是我目前的表:

print "<table cellpadding=12px>";

print "<th>Name</ th>";
print "<th>Price</ th>";
print "<th>Image</ th>";


if ($cat == 'Clothes'){

       $query = "SELECT * FROM Products WHERE Type = 'Clothes' ORDER BY ProductPrice";
   $result = mysqli_query($connection, $query);
   while ($row = mysqli_fetch_assoc($result)) 
   {
    echo " <tr>";
    echo "<td>" . $row['ProductName'] .  "</td>"; 
    echo "<td>" . '&pound'. $row['ProductPrice'] .  "</td>";
    echo "<td>" . '<img width=200px height=200px src="./ProductsPic/' . $row['ProductPic'] . '" />' . "</td>";

    echo "<td> <a href=cart.php?action=add&id=".$row["ID"].">Add to Cart</a></td>";
    echo "</tr>";

       }
   print "</table>";

然而,当我点击&#34;添加到购物车&#34;链接,它只将产品的ID发送到购物车。我不知何故需要发送该产品的价格和名称(可能隐藏数据?)。那么将表单集成到我的表中是否容易,或者是否可以通过其他方式将产品的名称和价格添加到我的购物车数组中?

1 个答案:

答案 0 :(得分:0)

如果ID字段是表示产品的唯一键,那么您只需从数据库中获取其他详细信息即可。否则,您可以执行以下操作:

您可以向查询字符串添加更多参数。

echo "<td> <a href=cart.php?action=add&id=".$row["ID"]."&price=".$row['ProductPrice']."&name=".$row['ProductName'].">Add to Cart</a></td>";

您可以使用隐藏值创建表单,并将添加到购物车链接转换为提交按钮。

echo '
<form action="cart.php">
    <input type="hidden" name="price" value="'.$row['ProductPrice'].'">
    <input type="hidden" name="name" value="'.$row['ProductName'].'">
    <input type="hidden" name="id" value="'.$row['ID'].'">
    <button type="submit">Add to cart</button>
</form>
';
相关问题