2 Versions (mysqli and mysql)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
CREATE TABLE `makemodel` ( `id` int(11) NOT NULL, `make` varchar(40) NOT NULL, `model` varchar(50) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `makemodel` (`id`, `make`, `model`) VALUES (1, 'ford', 'fiesta'), (2, 'bmw', '3 series'), (3, 'ford', 'fiesta'), (4, 'bmw', '3 series'); |
Mysqli version
dualdropdown.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<script src="https://code.jquery.com/jquery-1.8.3.js"></script> <script src="https://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> <?php include('dbconnect.php'); $sql = "select distinct make from makemodel"; if(!$result = $mysqli->query($sql)){ die('There was an error running the query [' . $mysqli->error . ']'); } ?> <select id="parent_category" name="parent_category" style=""> <?php echo "<option value=''>MAKE</option>"; while($row = mysqli_fetch_array($result)) { echo "<option value='".$row['make']."'>".ucfirst($row['make'])."</option>"; } ?> </select> <select id="child_category" name="child_category"> <option value=''>Please choose from above</option>"; </select> <script type="text/javascript"> $(document).ready(function() { $("#parent_category").change(function() { var pc_id = $(this).val(); if(pc_id != '') { $.ajax ({ type: "POST", url: "get_child.php", data: "pc_id="+ pc_id, success: function(option) { $("#child_category").html(option); } }); } else { $("#child_category").html("<option value=''>-- No category selected --</option>"); } return false; }); }); </script> |
get_child.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php include('dbconnect.php'); if(isset($_POST['pc_id']) && $_POST['pc_id'] != '') { $pc_id = $_POST['pc_id']; $pc_id = $mysqli->real_escape_string($pc_id); $sql = "select distinct model from makemodel where make='".$pc_id."'"; if(!$result = $mysqli->query($sql)){ die('There was an error running the query [' . $mysqli->error . ']'); } if(mysqli_num_rows($result)) { while($row = mysqli_fetch_array($result)) { echo "<option value='".$row['model']."'>".ucfirst($row['model'])."</option>"; } } } ?> |
older version mysql
dualdropdown.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<script src="https://code.jquery.com/jquery-1.8.3.js"></script> <script src="https://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> <?php include('cmdataconn.php'); $query = "select distinct make from makemodel"; $res = mysql_query($query); ?> <select id="parent_category" name="parent_category" style=""> <?php echo "<option value=''>MAKE</option>"; while($row = mysql_fetch_array($res)) { echo "<option value='".$row['make']."'>".ucfirst($row['make'])."</option>"; } ?> </select> <select id="child_category" name="child_category"> <option value=''>Please choose from above</option>"; </select> <script type="text/javascript"> $(document).ready(function() { $("#parent_category").change(function() { var pc_id = $(this).val(); if(pc_id != '') { $.ajax ({ type: "POST", url: "get_child.php", data: "pc_id="+ pc_id, success: function(option) { $("#child_category").html(option); } }); } else { $("#child_category").html("<option value=''>-- No category selected --</option>"); } return false; }); }); </script> |
get_child.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php include('cmdataconn.php'); if(isset($_POST['pc_id']) && $_POST['pc_id'] != '') { $pc_id = $_POST['pc_id']; $pc_id = mysql_real_escape_string($pc_id); $query = "select distinct model from makemodel where make='".$pc_id."'"; $res = mysql_query($query); if(mysql_num_rows($res)) { while($row = mysql_fetch_array($res)) { echo "<option value='".$row['model']."'>".ucfirst($row['model'])."</option>"; } } } ?> |