Janos_1 0 Newbie Poster

Hello, i have the following code:

index.php

<form role="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="products">Product</label>
<select id="products" name="products" class="user">
<?php 
    require 'data.php';
    $products = loadProducts();
    foreach ($products as $product) {
    echo "<option id='".$product['id']."' value='".$product['id']."' selected>".$product['name']."</option>";
}
?>
</select>
<label for="bundles">Bundle</label>
<select id="bundles" name="bundles">
</select>
</form>

Script

    <script type="text/javascript">
        $(document).ready(function(){
            $("#products").change(function(){
                var aid = $("#products").val();
                $.ajax({
                    url: 'data.php',
                    method: 'post',
                    data: 'aid=' + aid
                }).done(function(bundles){
                    console.log(bundles);
                    bundles = JSON.parse(bundles);
                    $('#bundles').empty();
                    bundles.forEach(function(bundle){
                        $('#bundles').append('<option>' + bundle.name + '</option>')
                    })
                })
            })
        })
    </script>

data.php

    <?php 
        require 'DbConnect.php';

        if(isset($_POST['aid'])) {
            $db = new DbConnect;
            $conn = $db->connect();

            $stmt = $conn->prepare("SELECT * FROM bundles WHERE pid = " . $_POST['aid']);
            $stmt->execute();
            $bundles = $stmt->fetchAll(PDO::FETCH_ASSOC);
            echo json_encode($bundles);
        }

        function loadProducts() {
            $db = new DbConnect;
            $conn = $db->connect();

            $stmt = $conn->prepare("SELECT * FROM products");
            $stmt->execute();
            $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
            return $products;
        }

     ?>
    </select>
    <label for="bundles">Bundle</label>
    <select id="bundles" name="bundles">
    </select>
    </form>

Now what i need is to how can i display the selected value in index.php because based on the values from the 2 lists i need to get a different value from a different table for example ( first list selected value is: prodname and second list selected is 7 now i need the 2 values to get a 3'rd value which is = prodname7)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.