0

I jut created a dynamic dropdown list that are dependant on each other on Comugg.com. Heres the code.

    function dropdownmenu() { 
include_once "connection.php";
?>
    <div class="make">
    <label>Make</label>
        <select name="make" id="makelist" onchange="getId(this.value);">
            <option value="">Select Make</option>

        <?php       
        $query = "select distinct(Make) from websitemasterlist order by Make ASC";
        $results = mysqli_query($conn, $query);

        foreach($results as $info) {
        ?>
            <option value="<?php echo $info[Make]; ?>"><?php echo $info[Make]; ?></option>

        <?php
        }
        ?>
        </select>
    </div>

    <div id="model">
    <label>Model</label>
        <select name="model" id="modellist" onchange="getId2(this.value);">
            <option value="">Select Model</option>
        </select>
    </div>

            <div id="year">
    <label>Year</label>
        <select name="year" id="yearlist" onchange="getId3(this.value);">
            <option value="">Select Year</option>
        </select>
    </div>

    <button id="dropdownbutton" onclick="dropdownbutton()" class="vc_general vc_btn3 vc_btn3-size-md vc_btn3-shape-rounded vc_btn3-style-3d vc_btn3-color-success">Dropdown</button>


    <script>
        function getId(val){
                jQuery.ajax({
                method: "POST",
                url: "getdata.php",
                data: "make="+val,
                success:function(data){
                    jQuery("#modellist").html(data);
                }
                });


        }
        function getId2(val){
                jQuery.ajax({
                method: "POST",
                url: "getdata.php",
                data: "model="+val,
                success:function(data){
                    jQuery("#yearlist").html(data);
                }
                });

        }

        function dropdownbutton(val){
        }

    </script>
<?php
}

and heres my getdata.php

<?php
include_once "connection.php";
$make = $_POST["make"];
$model= $_POST["model"];


if (!empty($_POST["make"])) {
    $make = $_POST["make"];
    echo $query = "SELECT Distinct Model FROM websitemasterlist where Make='".$make."'"; 
    $results = mysqli_query($conn, $query);
    ?>
    <option value="">Select Model</option>
    <?php
    foreach ($results as $info){
    ?>
    <option value="<?php echo $info["Model"]; ?>"><?php echo $info["Model"]; ?></option>
    <?php
    }
}

if (!empty($_POST["model"])) {
    $model = $_POST["model"];
    echo $model;
    echo $query = "SELECT Distinct Year FROM websitemasterlist where Model='".$model."'"; 
    $results = mysqli_query($conn, $query);
    ?>
    <option value="">Select Year</option>
    <?php
    foreach ($results as $info){
    ?>
    <option value="<?php echo $info["Year"]; ?>"><?php echo $info["Year"]; ?></option>
    <?php
    }
}
?>

Im trying to figure out how to go about creating a dynamic link. For example i wanted to do something like this but i am unsure of how to get the selected values from each field.

<a href="http://comugg.com/category/<?php echo $year; ?>/<?php echo $make; ?>/<?php echo $model; ?>>text</a>

How should i go about this?

  • Your code is vulnerable to [**SQL injection attacks**](https://en.wikipedia.org/wiki/SQL_injection). You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Apr 25 '17 at 18:31
  • thanks ill look into it afterwards – Charles Park Apr 25 '17 at 18:34

0 Answers0