从选中的单选按钮中获取值,并在 PHP 中添加到购物车


Get the value from the checked radio button and add to cart in PHP

我正在建立一个在线订购食物的网站。如果选中,我正在尝试从单选按钮中获取值。当我选中任一单选按钮并添加到购物车时,它总是只给我一个值。那么,如果选中单选按钮,如何获取该值?任何帮助都会很棒!谢谢。

这是我的代码。

数据库(表名为 items

id | product_code | product_name       | product_desc       | product_img_name | price | price2 | product_type
1  | AS01         | Dumpling           | Two for Each Order | 1.png            | 2.50  | NULL   | appitezer_soup
2  | HS02         | Chicken Fried Rice | ''                 | 2.png            | 5.50  | 7.50   | house_specials

鸡肉炒饭有两种尺寸Regular ($5.50)Large ($7.50)。在这里,如果选中单选按钮,我想添加到购物车。

索引.php

        $results = $mysqli->query("SELECT * FROM items WHERE product_type='house_specials' ORDER BY id ASC");
        if ($results) { 
        //fetch results set as object and output HTML
        while($obj = $results->fetch_object()) {
          echo '<div class="product">'; 
          echo '<form method="post" action="cart_update.php">';
          // echo '<div class="product-thumb"><img src="images/'.$obj->product_img_name.'"></div>';
          echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
          echo '<div class="product-desc">'.$obj->product_desc.'</div>';
          echo '<div class="product-info">';
          echo '<label for="pt">PT '.$currency.$obj->price.'</label><input id="pt" type="radio" name="productSize" value="PT"/>';
          echo '<label for="qt">QT '.$currency.$obj->price2.'</label><input id="qt" class="size_butt" type="radio" name="productSize" value="QT"/>';
          // echo 'Price: '.$currency.$obj->price.' | ';
          echo 'Qty <input type="text" name="product_qty" value="1" size="3" />';
          echo '<button class="add_to_cart">Add To Cart</button>';
          echo '</div></div>';
          echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
          echo '<input type="hidden" name="type" value="add" />';
          echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
          echo '</form>';
          echo '</div>';
        }
    } 
h2>Your Cart</h2>
<?php
if(isset($_SESSION["items"]))
{
    $total = 0;
    echo '<ol>';
    if (isset($_SESSION["items"])) {
    foreach ($_SESSION["items"] as $cart_itm)
    {
        echo '<li class="cart-itm">';
        echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
        echo '<h3>'.$cart_itm["name"].'</h3>';
        echo '<div class="p-code">P code : '.$cart_itm["code"].'</div>';
        echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
        echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
        echo '</li>';
        $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
        $total = ($total + $subtotal);
    }
    echo '</ol>';
    echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a href="view_cart.php"><button>Check-out!</button></a></span>';
    echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'"><button>Empty Cart</button></a></span>';
  }
} else {
   echo 'Your cart is empty';
}

cart_update

if(isset($_POST["type"]) || $_POST["type"]=='add')
{
    $product_code   = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
    $product_qty    = filter_var($_POST["product_qty"], FILTER_SANITIZE_NUMBER_INT); //product code
    $return_url     = base64_decode($_POST["return_url"]); //return url
    //MySqli query - get details of item from db using product code
    $results = $mysqli->query("SELECT product_name, price, price2 FROM items WHERE product_code='$product_code' LIMIT 1");
    $obj = $results->fetch_object();
    if ($results) { //we have the product info 
        //prepare array for the session variable
        $new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'qty'=>$product_qty, 'price'=>$obj->price, 'price2'=>$obj->price2));
        if(isset($_SESSION["items"])) //if we have the session
        {
            $found = false; //set found item to false
            foreach ($_SESSION["items"] as $cart_itm) //loop through session array
            {
                if($cart_itm["code"] == $product_code){ //the item exist in array
                    $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$product_qty, 'price'=>$cart_itm["price"], 'price2'=>$cart_itm["price2"]);
                    $found = true;
                }else{
                    //item doesn't exist in the list, just retrive old info and prepare array for session var
                    $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"], 'price2'=>$cart_itm["price2"]);
                }
            }
            if($found == false) //we didn't find item in array
            {
                //add new user item in array
                $_SESSION["items"] = array_merge($product, $new_product);
            }else{
                //found user item in array list, and increased the quantity
                $_SESSION["items"] = $product;
            }
        }else{
            //create a new session var if does not exist
            $_SESSION["items"] = $new_product;
        }
    }
    //redirect back to original page
    header('Location:'.$return_url);
}

你可以添加它

 $productSize = $_POST["productSize"]; 

在页面update_cart这些行下

    $product_code   = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
    $product_qty    = filter_var($_POST["product_qty"], FILTER_SANITIZE_NUMBER_INT); //product code
    $return_url     = base64_decode($_POST["return_url"]); //return url
    $productSize    = $_POST["productSize"]; // value of radio button

更改索引文件中的以下行

echo '<label for="pt">PT '.$currency.$obj->price.'</label><input id="pt" type="radio" name="productSize" value="PT"/>';
echo '<label for="qt">QT '.$currency.$obj->price2.'</label><input id="qt" class="size_butt" type="radio" name="productSize" value="QT"/>';

echo '<label for="pt">PT '.$currency.$obj->price.'</label><input id="pt" type="radio" name="productSize" value="'.$obj->price.'"/>';
echo '<label for="qt">QT '.$currency.$obj->price2.'</label><input id="qt" class="size_butt" type="radio" name="productSize" value="'.$obj->price2.'"/>';

并在更新购物车页面中更改以下行

$new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'qty'=>$product_qty, 'price'=>$obj->price, 'price2'=>$obj->price2));

$new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'qty'=>$product_qty, 'price'=>$_POST["productSize"], 'price2'=>$obj->price2));

希望这将解决问题