[英]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>
我会把它变成一个实际的形式。 这样,您可以选择产品,属性,然后使用提交按钮将其添加到购物车。
如果要保留此链接,则需要一些Java脚本将属性添加到链接中,但是基本上,您将使用脚本和链接来重新创建穷人的表格。
使用每个产品的实际表格,您可以轻松地让用户选择产品并将其添加到购物车,而无需任何脚本。
我添加的脚本只是为了显示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之类的浏览器辅助语言。 您知道可以在表单中使用表格吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.