Wednesday, 20 July 2016

Dynamic dropdown from database via Jquery

HTML File

 <tr>
            <th  scope="row">Select Brand:</th>
            <td>
            <select class="form-control" onChange="GetModel($brand)" id="brand" name="brand">
            <option value="">Select </option>
            <?php GetBrand(); ?>    
            </select>
            </td>
          </tr>
          <tr>
            <th scope="row">Phone Model</th>
            <td>
<div class="Xform-group">
<!-- <label class="control-label col-md-3">2</label>-->
<div class="Xcol-md-9">
<!-- <textarea class="wysihtml5 form-control" rows="6"  name="job_description" placeholder="Job Description" required></textarea>  -->
 <select class="form-control input-large" id="model" name="model">
      
          </select>
        </div>
</div>
</td>
          </tr>



Jquery for this 


<script>
$(document).ready(function(e) {
$("#brand").change(function(){
// alert('this is working');
$("#model").load("dynamic_data.php?brand=" + $("#brand").val());
});
});

</script>


dynamic_data.php


<?php 
include('db_config.php');
$brand = mysqli_real_escape_string($db,$_GET['brand']);
echo $brand;
$qry = "SELECT * FROM mobile_model WHERE brand = '$brand';";
$run = mysqli_query($db,$qry);

while($res = mysqli_fetch_array($run))
{
?>
    <option value="<?php echo $res['model_id'];  ?>">
    <?php
echo $res['model_name'];
?>
</option>

    <?php
}

?>

No comments:

Post a Comment