2
Save, I bugged here trying to make ajax bring me some data from a JSON and I need a lesson/help to understand how I pull the values of an object array(I think this is it rsrs). I have the following code below, it’s a bit confusing, but my goal is to do the following:
- Field "areaatu_02" was selected?
- yes: Then returns "select" from html with the values of "posses_title" that are in JSON
only that I never worked with a more complex structure of JSON, stirred with a simpler one ex: { "name": "Rafael Eduardo"}
Now I’m coming across array inside array and it’s bugging my head a lot...
Got very confused my doubt? follows below the code I tried to create:
json.php
$obj = ('{
"area_atuacao": [{
"areaatu_02": [{
"portador_titulo": [
"AMB",
"RESIDÊNCIA MÉDICA"
],
"possui_titulo": [
"MEDICINA PREVENTIVA E SOCIAL",
"ADMINISTRACAO EM SAUDE"
]
}],
"areaatu_04": [{
"portador_titulo": [
"AMB",
"RESIDÊNCIA MÉDICA"
],
"possui_titulo": [
"ALERGIA E IMUNOLOGIA",
"PEDIATRIA"
]
}],
"areaatu_07": [{
"portador_titulo": [
"AMB",
"RESIDÊNCIA MÉDICA"
],
"possui_titulo": [
"ANGIOLOGIA",
"CIRURGIA VASCULAR",
"RADIOLOGIA"
]
}]
}]
}
');
// converto em um Array
$myARRAY = json_decode($obj);
//coloco na tela
echo json_encode($myARRAY);
?>
php test.
<select name="AREA_ATUACAO" class="form-control" id="areaAtu" onchange="areaAtuFun()">
<option value="erro" disabled="disabled" selected>-- Selecione uma opção --</option>
<option value="areaatu_01">ACUPUNTURA</option>
<option value="areaatu_02">ADMINISTRAÇÃO EM SAÚDE</option><!-- TEM REGRA -->
<option value="areaatu_03">ALERGIA E IMUNOLOGIA</option>
<option value="areaatu_04">ALERGIA E IMUNOLOGIA PEDIÁTRICA</option><!-- TEM REGRA -->
<option value="areaatu_05">ANESTESIOLOGIA</option>
</select>
<div class='form-group col-md-4'>
<label for='PossueAreaAtu'>Portador do título de:</label>
<select name='PORTADOR_TITULO' class='form-control' id='PossueAreaAtu'>
<option value='erro' disabled='disabled' selected>-- Selecione uma opção --</option>
<span id="areaAtu_here"></span>
</select></div>
<script>
function areaAtuFun() {
var opFormValue = $('#areaAtu').val();
var opFormUrl = "json.php?data=area_atuacao."+ opFormValue;
$.ajax({
dataType: "json",
url: opFormUrl,
method: "GET",
data: data,
success: function(retorno){
if (retorno.count != 0) {
$(retorno.area_atuacao).appendTo("#areaAtu_here")
}else{
alert("Deu ruim.")
}
},
error: function(){
alert("Falha de conexão, tente novamente.")
}
})
var opForm = document.getElementById("areaAtu").value;
document.getElementById("areaAtu_here").innerHTML = "<option >"+ opForm +"</option>";
}
What do you want to put inside
.appendTo("#areaAtu_here")
?– Sergio
@Sergio a
<option>
with the value returned by ajax... OopForm
to be more precise.– Ursones
But ajax returns an object with an array, you want an option for each element of the array? with what content? can you exemplify this(s) option(s)?
– Sergio
@Sergio imagine that I selected the "areaatu_04" in my
select id="areaAtu"
, I want in theoption
it appears the options ofarea_atuacao.areaatu_04.possui_titulo
which is in JSON, more precisely:<option>ALERGIA E IMUNOLOGIA</option>
and<option>ADMINISTRACAO EM SAUDE</option>
– Ursones
But
ADMINISTRACAO EM SAUDE
is fromareaatu_02
... what is the logic of it appearing? wouldn’t it bePEDIATRIA
?– Sergio
@Sergio, is wrong, is not areaatu_04 was the same 2, I wrote wrong rsrs sorry... but the logic is this, show the options of what I select
– Ursones
Okay, and the old options disappear right?
– Sergio
that’s right, correct!
– Ursones
Okay, and this JSON has to have that format or can I suggest another?
– Sergio
@Sergio can do it, man, mine’s a bit fucked up
– Ursones
Let’s go continue this discussion in chat.
– Sergio