php变量中不带form标记的选定选项值


selected option value in php variable without form tag

我制作了一个购物车系统,其中数量正在成功更新,但现在我添加了两个新字段,即颜色和大小。我想在php变量中传递选定的值。我不想使用<form>标签。有可能这样做吗?我已经尝试了以下代码。有什么建议吗。

代码

<?php
$size = $_REQUEST['size'];//selected size
$color = $_REQUEST['color'];//selected color
if(isset($_GET['action']) && $_GET['action']=="add"){
    $id=intval($_GET['id']);
    if(isset($_SESSION['cart'][$id])){
        $_SESSION['cart'][$id]['quantity']++;
        $_SESSION['cart'][$id]['size'];//session for size
        $_SESSION['cart'][$id]['color'];//session for color
    }else{
       $sql_p="SELECT * FROM products WHERE productid={$id}";
        $query_p=mysqli_query($con, $sql_p);
        if(mysqli_num_rows($query_p)!=0){
            $row_p=mysqli_fetch_array($query_p);
            $_SESSION['cart'][$row_p['productid']]=array("quantity" => 1, "price" => $row_p['product_price'],"color"=>$color,"size"=>$size);
        }else{
            $message="Product ID is invalid";
        }
    }
}
?>
<table border="1">
    <tr>
        <th>Name</th>
        <th>Picture</th>
        <th>Description</th>
        <th>Price</th>
        <th>color</th>
        <th>size</th>
    </tr>
    <?php
    $query = mysqli_query($con,"SELECT * FROM products where cat_id=2 ORDER BY product_name ASC");
    while($row=mysqli_fetch_assoc($query)){
        ?>
        <tr>
            <td><?php echo $row['product_name']; ?></td>
            <td><img src="images/<?php echo $row['product_image']; ?>" width="120px" height="120px"></td>
            <td><?php echo $row['product_desc']; ?></td>
            <td><?php echo "$" . $row['product_price']; ?></td>
            <td>Colors:
                <select name="color">
                    <option selected value="choose">choose</option>
                    <option value="blue" id="blue">Blue</option>
                    <option value="yellow" id="yellow">Yellow</option>
                    <option value="green" id="green">Green</option>
                </select></td>
            <td> <select name="size"><option selected value="Choose size">Choose</option>
                    <option value="XL" id="XL">XL</option>
                    <option value="L" id="L">L</option>
                    <option value="S" id="S">S</option></select>
            </td>
            <td><a href="index.php?page=women&action=add&id=<?php echo $row['productid']; ?> ">Add to Cart</a></td>
        </tr>
    <?php
    }
    ?>
</table>

我会把它变成一个实际的表单。这样,你可以选择产品、属性,并使用提交按钮将其添加到购物车中。

如果你想保留这个链接,你需要一些Javascript来将属性添加到链接中,但你基本上是在使用脚本和链接重新创建一个穷人的表单。

使用每个产品的实际表单,您可以方便地让用户选择产品并将其添加到购物车中,而不需要任何脚本。

我添加的脚本只是为了显示url,因为Stack Snippets并没有让这一点变得很明显。

// Show the post url in the document, since forms are blocked in Stack Snippets.
$('form').on('submit', function() {
  document.write(
     'about to navigate to: ' + 
      $(this).attr('action') + '?' + 
      $(this).serialize());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="product" method="get" action="add.php">
  <input type="hidden" name="product" value="12345">
  <select name="color">
     <option value="red">red</option>
     <option value="green">red</option>
     <option value="blue">red</option>
  </select>
  <button type="submit">Add to cart</button>
</form>

我认为单独使用PHP无法实现您想要的功能。我认为您将不得不使用像JavaScript这样的浏览器端语言。你知道你可以在表格中使用表格吗?