Created many checkboxes where user can choose multiple checkboxes, than after posting I am taking it in an array and saving it in session array to store it temporary. I want retrieve the session array variable for comparing the values and printing the messages on website.
I am trying below code.
HTML
<form action="cart.php" method="POST">
<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">
PHP (cart.php)
<?php
session_start();
if(isset($_POST['submit']))
{
$oid = $_POST['oid'];
$addon = $_POST['addon_sel']; // ADDONS ARRAY.
$cart = array ('addon' => $addon);
$_SESSION['oid'] = $oid;
$_SESSION['cart'][$oid] = $cart;
echo "ITEMS IN YOUR CART<br>";
foreach ($_SESSION['cart'] as $item) {
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/-" ;
}
}
}
?>
But it is not showing me anything except the array which is printed using print_r()
. Please help and let me know if there is any other way to do so.