在没有表单标签的 php 变量中选择的选项值
selected option value in php variable without form tag
我制作了一个购物车系统,其中的数量已成功更新,但现在我添加了两个新字段,即颜色和尺寸。我想在 php variables.I 中传递选定的值不想使用 <form>
tag.Is 可以这样做吗?我试过下面的代码。有什么建议吗
代码
<?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>
我会把它转过来,让它成为一个实际的形式。这样,您可以 select 产品、属性并使用提交按钮将其添加到购物车。
如果你想保留这个 link,你将需要一些 Javascript 来将属性添加到 link,但是你基本上是在重新创建一个穷人的表单使用一个脚本和一个 link。
使用每个产品的实际表格,您可以轻松地让用户 select 产品并将它们添加到购物车,而无需任何脚本。
我添加的脚本只是为了显示 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 这样的浏览器端语言。
您知道可以在表单中使用表格吗?
我制作了一个购物车系统,其中的数量已成功更新,但现在我添加了两个新字段,即颜色和尺寸。我想在 php variables.I 中传递选定的值不想使用 <form>
tag.Is 可以这样做吗?我试过下面的代码。有什么建议吗
代码
<?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>
我会把它转过来,让它成为一个实际的形式。这样,您可以 select 产品、属性并使用提交按钮将其添加到购物车。
如果你想保留这个 link,你将需要一些 Javascript 来将属性添加到 link,但是你基本上是在重新创建一个穷人的表单使用一个脚本和一个 link。
使用每个产品的实际表格,您可以轻松地让用户 select 产品并将它们添加到购物车,而无需任何脚本。
我添加的脚本只是为了显示 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 这样的浏览器端语言。 您知道可以在表单中使用表格吗?