Search Posts

How to change SQL query with three different PHP variables without refreshing page

I have a huge problem.

I’m creating an e-commerce site.

I want to change this HTML value dinamically:

<h5 class="item_price">
    500.00  <----- THIS IS THE PRICE I WANT TO CHANGE -->
</h5>

when the user changes one, two or all of these ComboBoxes:

<ul>
    <li class="size-in"><i>Thickness</i>:
        <select>
            <?php
            for ($i = 0; $i < count($thicknessesRow); $i++) {
                echo '<option>' . $thicknessesRow[$i]['product_thickness'] . ' mm</option>';
            }
            ?>
        </select>
    </li>
    <li class="size-in"><i>Diameter</i>:
        <select>
            <?php
            for ($i = 0; $i < count($diametersRow); $i++) {
                echo '<option>' . $diametersRow[$i]['product_diameter'] . ' mm</option>';
            }
            ?>
        </select>
    </li>
    <li class="size-in"><i>Meters</i>:
        <select>
            <?php
            for ($i = 0; $i < count($metersRow); $i++) {
                echo '<option>' . $metersRow[$i]['product_meters'] . '</option>';
            }
            ?>
        </select>
    </li>
    <div class="clearfix"> </div>
</ul>

These are the queries used for displaying the Combos values:

<?php
$thicknesses = $DB_con->prepare('SELECT DISTINCT product_thickness FROM products WHERE product_subcat = 2 ORDER BY product_thickness');
$thicknesses->execute();
$thicknessesRow = $thicknesses->fetchAll(PDO::FETCH_ASSOC);

$diameters = $DB_con->prepare('SELECT DISTINCT product_diameter FROM products WHERE product_subcat = 2 ORDER BY product_diameter');
$diameters->execute();
$diametersRow = $diameters->fetchAll(PDO::FETCH_ASSOC);

$meters = $DB_con->prepare('SELECT DISTINCT product_meters FROM products WHERE product_subcat = 2 ORDER BY product_meters');
$meters->execute();
$metersRow = $meters->fetchAll(PDO::FETCH_ASSOC);
?>

The price (which values are contained in ‘products’ table) has to change dinamically, because it depends by ‘thickness’, ‘diameter’ and ‘meters’.

I suppose I have to make different queries using the three PHP variables ($thickness, $diameters, $meters);

But how can I do it without forcing user to reload the page every time?

Do I have to use AJAX?

Can you explain me how, according to this code?

Because I have no idea how to do it.

Thanks a lot for your answers.

Source: Stackoverflow

Leave a Reply

Your email address will not be published. Required fields are marked *