I'm using a form to record dynamic questions in my system. I'm also recording this data in a session so you do not have to be selecting all the time in dropdows but you have a problem:
In submit , in the Select a category: field, I get the category ID in the session and auto-select it with PHP and give it a jQuery trigger so that through of the Category ID, it queries the database and populates the Select an activity dropdown: ...
Now comes the trick: I also want to leave Activity selected because are several questions for the same Category and Activity.
At load, the system takes the selected ID in Category and queries Activity . What I have so far are two ID's in a session (PHP) and the jQuery code below:
$(document).ready(function() {
var param = $("#categoriaCad option:selected").val();
$.ajax({
url: CAMINHO_JAVASCRIPTI+"/usuarios/ajaxsubcat",
type: "POST",
dataType: "json",
data: {param: param},
success: function(data){
$('#cadSubcategoria').find('option').remove().end().append('<option value="">Selecione uma categoria ...</option>');
$.each(data, function(chave, valor) {
$('#cadSubcategoria').append($('<option>').attr('value', valor.id).text(valor.atividade));
});
}
});
});