我正在为多个产品创建一个网站,其中有4个属性正面,背面,侧面和插件(这是收费的),下面创建了一个简单的演示代码。
属性页(prop.php)
<html>
<head>
</head>
<body>
YOU HAVE CHOOSED <h1>PRODUCT 1</h1>
<h2>CHOOSE PRODUCT PROPERTIES</h2>
<form action="cartpage.php" method="post">
ORDER ID <input type="text" id="uid" name="oid" value="<?php echo uniqid(); ?>"><br><br>
<label>FRONT SELECTION</label><br>
<input type="radio" name="front_sel" value="front_1">FRONT 1<br>
<input type="radio" name="front_sel" value="front_2">FRONT 2<br>
<input type="radio" name="front_sel" value="front_3">FRONT 3<br>
<input type="radio" name="front_sel" value="front_4">FRONT 4<br><br>
<label>BACK SELECTION</label><br>
<input type="radio" name="back_sel" value="back_1">BACK 1<br>
<input type="radio" name="back_sel" value="back_2">BACK 2<br>
<input type="radio" name="back_sel" value="back_3">BACK 3<br>
<input type="radio" name="back_sel" value="back_4">BACK 4<br><br>
<label>SIDE SELECTION</label><br>
<input type="radio" name="side_sel" value="side_1">SIDE 1<br>
<input type="radio" name="side_sel" value="side_2">SIDE 2<br>
<input type="radio" name="side_sel" value="side_3">SIDE 3<br>
<input type="radio" name="side_sel" value="side_4">SIDE 4<br><br>
<label>ADD ON SELECTION</label><br>
<input type="checkbox" name="addon_sel[]" value="addon_1">THIS ADDON COST Rs 50 EXTRA<br>
<input type="checkbox" name="addon_sel[]" value="addon_2">THIS ADDON COST Rs 20 EXTRA<br>
<input type="checkbox" name="addon_sel[]" value="addon_3">THIS ADDON COST Rs 0 EXTRA<br>
<input type="checkbox" name="addon_sel[]" value="addon_4">THIS ADDON COST Rs 10 EXTRA<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
php购物车页面(cartpage.php)
<?php
if(isset($_POST['submit']))
{
$oid = $_POST['oid'];
$front = $_POST['front_sel'];
$back = $_POST['back_sel'];
$side = $_POST['side_sel'];
$addon = $_POST['addon_sel'];
$cart = array (
'front' => $front,
'back' => $back,
'addon' => $addon,
'side' => $side
);
$_SESSION['oid'] = $oid;
$_SESSION['cart'][$oid] = $cart;
echo "ITEMS IN YOUR CART<br>";
foreach ($_SESSION['cart'] as $item) {
echo $item['front']."<br>";
echo $item['back']."<br>";
echo $item['side']."<br>";
echo $item['addon']."<br>";
if ($item['addon'] == 'addon_1')
{
echo "TOTAL PRICE OF THE PRODUCT IS Rs. 500 + Price of ADDON Rs 50 = Rs. 550/-" ;
}if ($item['addon'] == 'addon_2')
{
echo "TOTAL PRICE OF THE PRODUCT IS Rs. 500 + Price of ADDON Rs 20 = Rs. 520/-" ;
}if ($item['addon'] == 'addon_3')
{
echo "TOTAL PRICE OF THE PRODUCT IS Rs. 500 + Price of ADDON Rs 0 = Rs. 500/-" ;
}if ($item['addon'] == 'addon_1')
{
echo "TOTAL PRICE OF THE PRODUCT IS Rs. 500 + Price of ADDON Rs 10 = Rs. 510/-" ;
}
}
}
?>
首先: 在上面的代码中,每当用户检查多个复选框时,只需发布一个复选框。
第二 我想在会话中使用多个复选框来创建购物车系统并为其指定价格。 希望你能理解这个问题请帮助我,请告诉我任何其他方式做同样的事情。感谢。
答案 0 :(得分:0)
您的复选框都具有相同的name="addon_sel"
。 PHP的多个字段具有相同名称的默认行为是仅保存LAST遇到的值。使用name="addon_sel[]"
(注意[]
)告诉PHP创建一个接收到的值数组。
答案 1 :(得分:0)
我对您的代码进行了一些更改以使其正常工作,这些更改由箭头指出(//<=================
):
<?php
session_start(); //<=================
if(isset($_POST['submit']))
{
$oid = $_POST['oid'];
$front = $_POST['front_sel'];
$back = $_POST['back_sel'];
$side = $_POST['side_sel'];
$addon = $_POST['addon_sel']; // ADDONS ARRAY.
$cart = array ( 'front' => $front,
'back' => $back,
'addon' => $addon,
'side' => $side
);
$_SESSION['oid'] = $oid;
$_SESSION['cart'][$oid] = $cart;
echo "ITEMS IN YOUR CART<br>";
foreach ($_SESSION['cart'] as $item) {
echo $item['front']."<br>";
echo $item['back']."<br>";
echo $item['side']."<br>";
print_r( $item['addon'] )."<br>"; //<=================
if ( in_array( 'addon_1',$item['addon'] ) ) //<=================
{
echo "<br/>TOTAL PRICE OF THE PRODUCT IS Rs. 500 + Price of ADDON Rs 50 = Rs. 550/-" ;
}if ( in_array( 'addon_2',$item['addon'] ) ) //<=================
{
echo "<br/>TOTAL PRICE OF THE PRODUCT IS Rs. 500 + Price of ADDON Rs 20 = Rs. 520/-" ;
}if ( in_array( 'addon_3',$item['addon'] ) ) //<=================
{
echo "<br/>TOTAL PRICE OF THE PRODUCT IS Rs. 500 + Price of ADDON Rs 0 = Rs. 500/-" ;
}if ( in_array( 'addon_4',$item['addon'] ) ) //<=================
{
echo "<br/>TOTAL PRICE OF THE PRODUCT IS Rs. 500 + Price of ADDON Rs 10 = Rs. 510/-" ;
}
}
}
?>