Maybe what I'm asking is something very obvious, but as I'm learning I prefer to consult. When I have a form with all the complete data and send me an error message, the input fields bring back the written data to not write it again, but in the combobox the same thing does not happen to me, as I do to rescue the Selected option.
I have the following code
<select name="categoria" id="categoria">
<option value=""></option>
<?php
if($data!=null) {
foreach ($data->data as $key => $value) {
if($value->Id==$pdata['categoria']){
echo '<option selected value="'.$value->Id .'">' . $value->Label . '</option>';
}else{
echo '<option value="'.$value->Id .'">' . $value->Label . '</option>';
}
}
}else{
echo '<option value="">--</option>';
}
?>
</select>
<select name="subcat" id="subcat">
<option value="">--</option>
</select>
<script>
$(document).ready(function (){
$('#categoria').change(function (){
getSubject($(this).val());
});
function getSubcat(valor){
$.getJSON( "getSubcat.php", { sub: valor }, function( data ) {
var items = [];
$('#subcat').find('option').not(':first').remove();
$.each( data['Subcat'], function( key, val ) {
$('#subcat').append($('<option>', {
value: val.Subcat,
text : val.Subcat
}));
});
});
}
<?php
if($pdata['categoria']!=""){
echo "getSubcat(".$pdata['categoria'].");";
}
?>
});
Please, if you can help me. Thanks