mardi 13 juin 2023

Trying to print only radio button’s data from cookie in which selected data stored But it’s printing both checkbox and radio button’s stored data

We have checkboxes and radio button in our product page as data , we are store selected data in cookie and when user click on continue button page will redirect to cart page , on cart page we are printing selected stored data in card but when we only want radio button’s stored selected data to be print but using my code it’s printing both checkbox and radio button’s selected data.

here is full code of our cart page ,

prepare("SELECT sub_name, sem_name, price FROM selected_bcom_values"); $stmt->execute(); $selectedData = $stmt->fetchAll(); ?>

<!doctype html>

Include your stylesheets and scripts here --> .card { background-color: white; width: auto; height: auto; border: 0px; border-radius: 30px; padding: 20px; padding-left: 0px; box-shadow: 0 8px 30px -7px #c9dff0; margin: 20px; text-align: left; /* Align cards to the left */ font-family: 'Ubuntu', sans-serif; font-size: 1rem; font-weight: 500; line-height: 1.3; position: relative; }

    .container {

display: flex; flex-direction: row; justify-content: space-between; /* Align cards and order summary / align-items: flex-start; / Align cards to the left */ }

    .col-md-7 {

flex: 0 0 auto; /* Prevent cards from growing to fill the space */ }

    .order-summary {

flex: 0 0 auto; /* Prevent order summary from growing to fill the space / padding-top: 100px; / Add top padding */ }

</style>



<div class="container">

    <div class="col-md-7">


        <br>
        <br>
        <br>
        <?php

Check if the selected data cookie exists if (isset($_COOKIE['selectedData'])) { Retrieve the selected data from the cookie $selectedData = json_decode($_COOKIE['selectedData'], true);

Display the selected data echo "

";

Flag to track if the "Remove" line card has been printed $removeLinePrinted = false;

Counter for the selected checkboxes $selectedCount = 0;

Loop through the selected data and display each item in a card format foreach ($selectedData as $sub_name => $data) { Print the "Remove" line card only once at the top if (!$removeLinePrinted) { echo ""; echo ""; echo " "; echo " "; echo " <label class='form-check-label' style='font-size: .77rem;font-family: 'Varela Round', serif;font-weight:bold;'>$selectedCount/ ITEMS SELECTED"; echo " "; echo " <div id='remove-all-item' style='cursor: pointer;font-family: 'Varela Round', serif;font-weight:bold;'>REMOVE"; echo ""; echo "";

                        $removeLinePrinted = true;
                    }

Open the card div for the current item echo "";

Print the rest of the card content for the current item

echo ""; echo " "; echo " "; echo " "; echo " "; echo ""; echo "

" . str_replace('_', ' ', $sub_name) . "
";

Check if the $data variable is an array if (is_array($data)) { foreach ($data as $material_name => $attempts) { echo "

" . str_replace('_', ' ', $material_name) . "
";

Check if the $attempts variable is an array if (is_array($attempts)) { Find the radio button selection $radioSelection = null; foreach ($attempts as $attempt_name => $price) { if (strpos($attempt_name, 'radio_') !== false) { $radioSelection = $attempt_name; break; } }

Display the radio button selection, if found if ($radioSelection !== null) { $price = str_replace('', ' ', $attempts[$radioSelection]); $firstPart = substr($price, 0, 3); $secondPart = substr($price, 3); echo "Selected Attempt Name: " . str_replace('', ' ', $radioSelection) . "

"; echo "Selected Attempt Price: $firstPart
$secondPart"; } else { If $attempts is not an array, assume it's the price and display it $price = str_replace('_', ' ', $attempts); $firstPart = substr($price, 0, 3); $secondPart = substr($price, 3, -5); echo "

" . $secondPart . "₹ " . $firstPart . "

";
                                //echo "<h5 class='price'> <span style='text-align: right;'>₹ $firstPart</span><br><span>$secondPart</span></h5>";
                            }
                        }

else { If $data is not an array, assume it's the price and display it $price = str_replace('_', ' ', $data); $firstPart = substr($price, 0, 3); $secondPart = substr($price, 3); echo "₹ $firstPart

"; // Display $firstPart on the right side echo " ₹ $firstPart
$secondPart"; } echo ""; echo " '>"; echo "";

Close the card div for the current item echo ""; }

else { echo "

No data selected.

"; } ?>
    <div class="order-summary">
        <div style="border: 1px solid #dcdcdc;padding: 1rem 1rem;background-color: #FFF;">
            <div style="font-family: 'Ubuntu', serif;">
                <h3 style="font-weight:200;">Order Summary</h3>
                <span style="display:none;float: right;"><b>₹ <span id="total_price"></span></b></span>
                <hr style="background-color: #babfbb;">
                <h6 style="text-align: left;">
                    <div class="form-check" style="font-size:1.05rem;display:inline-block;">
                        <label class="form-check-label h-100 align-middle" for="physical_notes">Printable PDFs &nbsp;</label>
                    </div>
                    <!-- <b>₹ <span style="float: right;"><span id="total_price2"></span></span></b> -->
                    <span style="float: right;"> <b>₹ <span id="total_price2"><?php echo $_COOKIE['total_price']; ?></span></b></span>
                </h6>
                <div class=" d-md-block">
                    <hr style="background-color: #babfbb;">
                        <!-- <h6 style="text-align: left;">
                            <div class="form-check" style="font-size:1.05rem;display:inline-block;">
                                <label class="form-check-label h-100 align-middle" for="physical_notes">Total Price &nbsp;</label>
                            </div>
                             <b>₹ <span style="float: right;"><span id="total_price2"></span></span></b> 
                            <span style="float: right;"> <b>₹ <span id="total_price2"><//?php echo $_COOKIE['total_price']; ?></span></b></span>
                        </h6> -->

                    <button class="btn btn-danger" style="background-color: #f05969;color: #fff;width:100%;border-color:#f05969;" id="continue">CONTINUE</button>
                </div>
            </div>
        </div>
    </div>
</div>

Include your scripts here --> Get all the purchase checkboxes const purchCheckboxes = document.querySelectorAll('.purch-checkbox');

Get the total_selected_item element const totalSelectedElement = document.getElementById('total_selected_item');

Update the selected count when a checkbox is checked or unchecked function updateSelectedCount() { const selectedCount = Array.from(purchCheckboxes).filter(checkbox => checkbox.checked).length; totalSelectedElement.textContent = selectedCount; }

Add event listeners to the checkboxes purchCheckboxes.forEach(checkbox => { checkbox.addEventListener('change', updateSelectedCount); });




Aucun commentaire:

Enregistrer un commentaire