添加单选按钮值以使用PHP添加到购物车产品价格

时间:2014-01-20 11:25:46

标签: php mysql cart

我从前6个小时开始挣扎,但没有找到解决方案。 我已经创建了一些额外的字段,添加到购物车按钮,基本上是单选按钮。 我正在通过他们的新会话获得这些单选按钮的值 HTML

$radio_price = "";
if (isset($_POST['radio_price'])){
$_SESSION['radio_price'] = $_POST['radio_price'];
}
if (isset($_SESSION['radio_price'])){
$radio_price = $_SESSION['radio_price'];
}

<input type="radio" name="radio_price" value="45"> 4 Business Days
<input type="radio" name="radio_price" value="35"> 3 Business Days
<input type="radio" name="radio_price" value="25"> 2 Business Days
<input type="radio" name="radio_price" value="15"> 1 Business Days

添加到购物车按钮

<a href="cart.php?action=add&id=<?php echo $profile_data['id']; ?>" class="addtocart">Addto cart </a>

选择值:

<?php echo $radio_price; ?>

我还创建了文件名为radio_id =(int)和radio_price =(varchar)

的数据库

PHP cart.php代码

$cart = $_SESSION['cart'];
$action = $_GET['action'];
witch ($action) {
case 'add':
    if ($cart) {
        $cart .= ','.$_GET['id'];
    } else {
        $cart = $_GET['id'];
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=cart.php">';
    }
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=cart.php">';
    break;
case 'delete':
    if ($cart) {
        $items = explode(',',$cart);
        $newcart = '';
        foreach ($items as $item) {
            if ($_GET['id'] != $item) {
                if ($newcart != '') {
                    $newcart .= ','.$item;
                } else {
                    $newcart = $item;
                }
            }
        }
        $cart = $newcart;
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=cart.php">'; 
    }
    break;
case 'update':
if ($cart) {
    $newcart = '';
    foreach ($_POST as $key=>$value) {
        if (stristr($key,'qty')) {
            $id = str_replace('qty','',$key);
            $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
            $newcart = '';
            foreach ($items as $item) {
                if ($id != $item) {
                    if ($newcart != '') {
                        $newcart .= ','.$item;
                    } else {
                        $newcart = $item;
                        echo '<META HTTP-EQUIV="Refresh" Content="0; URL=cart.php">'; 
                    }
                }
            }
            for ($i=1;$i<=$value;$i++) {
                if ($newcart != '') {
                    $newcart .= ','.$id;
                } else {
                    $newcart = $id;
                }
            }
        }
    }
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;

现在,当用户点击添加到购物车时,目标是在产品价格中添加$radio_price;值。

0 个答案:

没有答案