Skip to content

Instantly share code, notes, and snippets.

@leeogrady
Created January 4, 2017 21:59
Show Gist options
  • Save leeogrady/32c54e41f4eb57aeafbf996efc72e114 to your computer and use it in GitHub Desktop.
Save leeogrady/32c54e41f4eb57aeafbf996efc72e114 to your computer and use it in GitHub Desktop.
// HTML //
<div class="col-md-6">
<div for='typeId' class="form-group">
<label for='typeId' class="control-label" >Type</label>
<select class="form-control" name='typeId' id='typeId' onchange='updateModel()' required >
<option value=''>Select Type</option>
<?php
$qry = "SELECT * FROM trailerType ORDER BY name";
if(!$res = $dbc->query($qry)) {
echo "Unavailable";
} else {
while($d = $res->fetch_assoc()) {
echo "<option value='".$d['id']."'";
if($type_id == $d['id']) {
echo " SELECTED='selected'";
}
echo ">".$d['name']."</option>";
}
$res->free();
}
?>
</select>
</div>
</div>
<div class="col-md-6">
<div for='model_id' class="form-group">
<label for='model_id' class="control-label">Model</label>
<select class="form-control" name='model_id' id='model_id' required>
<option value='' typeId=''>Select model</option>
<?php
$qry = "SELECT * FROM trailerModel ORDER BY name";
if(!$res = $dbc->query($qry)) {
echo "Unavailable";
} else {
while($d = $res->fetch_assoc()) {
echo "<option typeId='".$d['typeId']."' value='".$d['id']."'";
if($model_id == $d['id']) {
echo " SELECTED='selected'";
}
echo ">".$d['name']."</option>";
}
$res->free();
}
?>
</select>
</div>
</div>
//JAVASCRIPT //
function updateModel() {
var type = $('#typeId').val();
$('#model_id option').each(function() {
if(($(this).attr('typeid') == type) || ($(this).attr('typeid') == '')) {
$(this).css('display','block');
} else {
$(this).css('display','none');
}
});
if($('#model_id :selected').attr('typeid') != type) {
$('#model_id').val('');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment