简体   繁体   中英

Printing session multiple value of a checkbox array in php

I am creating a website for multiple products which have 4 properties front, back, side and addon (which is chargeable), created a simple demo below is the code.

PROPERTIES PAGE(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 cart page (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/-" ;
         }
     }
}
?>

First : In the above code whenever user checks on multiple checkboxes it takes only post one checkbox.

Second I want multiple checkboxes in the session for creating a cart system and appointing price to them. Hope you understand the problem Please help me and please tell me any other way for doing the same. Thanks.

Your checkboxes all have the same name="addon_sel" . PHP's default behavior for multiple fields with the same name is to save only the LAST encountered value. Use name="addon_sel[]" (note the [] ) to tell PHP to create an array of values received.

I made some changes in your code to make it work, those changes are pointed by arrows ( //<================= ) :

<?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/-" ;
         }
     }
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM