-2
The javascript code below fills in the select "regions" (id) options of the json file "list-region-programs-municipio". But in the html form this fills the value with ID value in json. And if you change it does not work select.
HtmlOption += '<option value="' + select.id + '">' + select.name + '</option>';
How do I php get the text?
Html form
<form method="POST" action="SIGPLAM4/cadastro.php" role="form">
<div class="form-group" role="select">
<label for="regioes">Região de integração</label>
<select name="regioes" id="regioes">
<option value="">Selecione a regição</option>
</select>
</div>
<div class="form-group" role="selecet">
<!-- selecionar o programa -->
<label for="programas">Programa desenvolvido</label>
<select name="programas" id="programas">
<option value="">Selecione o programa</option>
</select>
</div>
<input type="submit" class="btn btn-primary" value="Cadastrar">
Archive: alimentary-combos-regiao-programas-municipio.js
function get_json_data(id, parent_id) {
var HtmlOption = '';
$.getJSON('SIGPLAM4/javaScript/lista-regiao-programas-municipio.json', function(data){
ListSelect = id; // ID do select regioes do form html
HtmlOption += '<option value="">Selecione a ' + ListSelect + '</option>';
// alimenta o objeto select usando each e, uma função anônima de callback (índice do objeto e o objeto)
$.each(data, function(key, select) {
if(select.parent_id == parent_id) {
HtmlOption += '<option value="' + select.id + '">' + select.name + '</option>';
}
});
$('#' + id).html(HtmlOption); // envia as opções do select do arquivo json ao form html
});
}
get_json_data('regioes', 0);
$(document).on('change', '#regioes', function(){ // quando uma opção é selecionada no select regioes, alimenta os outros selects do form
var regioes_id = $(this).val();
if (regioes_id != ''){
get_json_data('programas', regioes_id);
get_json_data('municipio', category_id);
get_json_data('municipioSuas', category_id);
get_json_data('municipioVig', category_id);
} else {
$('#programas').html('<option value="">Selecione o programa</option>');
}
});
$(document).on('change', '#programas', function(){
var category_id = $(this).val();
if (category_id != '') {
get_json_data('municipio', category_id);
get_json_data('municipioSuas', category_id);
get_json_data('municipioVig', category_id);
} else {
$('#municipio').html('<option value="">Selecione o municipio</option>');
$('#municipioSuas').html('<option value="">Selecione o municipio SUAS</option>');
$('#municipioVig').html('<option value="">Selecione o municipio VIG</option>');
}
});
File list-region-programs-municipio.json
[
{
"id":"1",
"name":"Araguaia",
"parent_id":"0"
},
{
"id":"2",
"name":"Baixo Amazonas",
"value":"Baixo Amazonas",
"parent_id":"0"
},
{
"id":"3",
"name":"Carajás",
"parent_id":"0"
},
{
"id":"4",
"name":"Programa 1",
"parent_id":"1"
},
{
"id":"5",
"name":"Programa 2",
"parent_id":"1"
},
{
"id":"6",
"name":"Programa 3",
"parent_id":"1"
},
{
"id":"7",
"name":"Agua Azul do Norte",
"parent_id":"4"
},
{
"id":"8",
"name":"Bannach",
"parent_id":"4"
},
{
"id":"9",
"name":"Conceição do Araguaia",
"parent_id":"4"
},
{
"id":"10",
"name":"Agua Azul do Norte",
"parent_id":"5"
},
{
"id":"11",
"name":"Bannach",
"parent_id":"5"
},
{
"id":"12",
"name":"Conceição do Araguaia",
"parent_id":"5"
}
]
A simple PHP
if (isset($_POST['regioes']) ? $_POST['regioes'] : null){
'<option value="' + select.id + '">' + select.name + '</option>'; I found this code on the net. In the original value is receiving the "select.id" and the php "post" saves a number in mysql... But if I change p/ "select.value" the select programs and select form municipio are not fed with the json options. Changing the php "post" saves the text in mysql but the other selects are empty.
– Eli Lopes
From what you have said, you cannot change the "select.id" so that your system keeps running. What is your question then? Do you want to display the region name after the record is saved? If so, you should not change the form code. You must change the code that shows the information.
– Bruno
I cannot change the "select.id" of the command below because the system stops working Htmloption += '<option value="' + select.id + '">' + select.name + '</option>'; So where do I put the "select.value" in the $.each javascript? I need to save the "value" or "name" of select in mysql. Since it is only saved the "select.id" number"
– Eli Lopes