0
Colleagues.
Driving from here and there, I reached the code below. His objective is to select the state, to appear the cities referring to that state. So far so good, but I need to make sure that when editing, the cities appear and of course, the registered city is selected. The code is this:
To the popular state Combox coming from the bank and select the desired state, I have the following code:
$(function() {
$("#estados").change(function() {
var id = $(this).val();
$.ajax({
type: "POST",
url: "exibe-cidades.php?id=" + id,
dataType: "text",
success: function(res) {
$("#cidades").children(".cidades").remove();
$("#cidades").append(res);
}
});
});
});
Combox is inside a PHP method like this:
public function listarCidades($id){
$sql = mysqli_query($this->conexao,"SELECT cod_estados, sigla FROM crm_estados ORDER BY sigla");
$mostrar = "<select name=\"Estados\" class=\"select1_single form-control\" id=\"estados\">";
$mostrar .= "<option value=''></option>";
while($listar = mysqli_fetch_array($sql)){
$selected = ($id == $listar['cod_estados'])?("selected"):(null);
$mostrar .= "<option value=".$listar['cod_estados']." ".$selected.">".$listar['sigla']."</option>";
}
$mostrar .= "</select>";
return $mostrar;
}
I don’t think the solution is in the.php cities file, but in this section. Would anyone have any idea?
The Cities Box is below:
<option value="1" class="cidades">ACRELÂNDIA</option>
<option value="2" class="cidades">ASSIS BRASIL</option>
<option value="3" class="cidades">BRASILÉIA</option>
<option value="4" class="cidades">BUJARI</option>
<option value="11059" class="cidades">CAMPINAS</option>
<option value="5" class="cidades">CAPIXABA</option>
<option value="6" class="cidades">CRUZEIRO DO SUL</option>
<option value="7" class="cidades">EPITACIOLÂNDIA</option>
<option value="8" class="cidades">FEIJÓ</option>
<option value="9" class="cidades">JORDÃO</option>
<option value="10" class="cidades">MÂNCIO LIMA</option>
<option value="11" class="cidades">MANOEL URBANO</option>
<option value="12" class="cidades">MARECHAL THAUMATURGO</option>
<option value="13" class="cidades">PLÁCIDO DE CASTRO</option>
<option value="14" class="cidades">PORTO ACRE</option>
<option value="15" class="cidades">PORTO WALTER</option>
<option value="16" class="cidades">RIO BRANCO</option>
<option value="17" class="cidades">RODRIGUES ALVES</option>
<option value="18" class="cidades">SANTA ROSA</option>
<option value="19" class="cidades">SENA MADUREIRA</option>
<option value="20" class="cidades">SENADOR GUIOMARD</option>
<option value="21" class="cidades">TARAUACÁ</option>
<option value="22" class="cidades">XAPURI</option>
<option value="191" class="cidades">ALVARÃES</option>
<option value="192" class="cidades">AMATARI</option>
<option value="193" class="cidades">AMATURÁ</option>
<option value="194" class="cidades">ANAMÃ</option>
<option value="195" class="cidades">ANORI</option>
<option value="196" class="cidades">APUÍ</option>
<option value="197" class="cidades">ARIAÚ</option>
<option value="198" class="cidades">ATALAIA DO NORTE</option>
<option value="199" class="cidades">AUGUSTO MONTENEGRO</option>
<option value="200" class="cidades">AUTAZES</option>
<option value="201" class="cidades">AXINIM</option>
<option value="202" class="cidades">BADAJÓS</option>
<option value="203" class="cidades">BALBINA</option>
<option value="204" class="cidades">BARCELOS</option>
<option value="205" class="cidades">BARREIRINHA</option>
<option value="206" class="cidades">BENJAMIN CONSTANT</option>
<option value="207" class="cidades">BERURI</option>
<option value="208" class="cidades">BOA VISTA DO RAMOS</option>
<option value="209" class="cidades">BOCA DO ACRE</option>
<option value="210" class="cidades">BORBA</option>
<option value="211" class="cidades">CAAPIRANGA</option>
<option value="10471" class="cidades">CACAU PIRÊRA</option>
<option value="212" class="cidades">CAMETÁ</option>
<option value="213" class="cidades">CANUMÃ</option>
<option value="214" class="cidades">CANUTAMA</option>
<option value="215" class="cidades">CARAUARI</option>
<option value="216" class="cidades">CAREIRO</option>
<option value="217" class="cidades">CAREIRO DA VÁRZEA</option>
<option value="218" class="cidades">CARVOEIRO</option>
<option value="219" class="cidades">COARI</option>
<option value="220" class="cidades">CODAJÁS</option>
<option value="221" class="cidades">CUCUÍ</option>
<option value="222" class="cidades">EIRUNEPÉ</option>
<option value="223" class="cidades">ENVIRA</option>
<option value="224" class="cidades">FLORIANO PEIXOTO</option>
<option value="225" class="cidades">FONTE BOA</option>
<option value="226" class="cidades">FREGUESIA DO ANDIRÁ</option>
<option value="227" class="cidades">GUAJARÁ</option>
<option value="228" class="cidades">HUMAITÁ</option>
<option value="229" class="cidades">IAUARETÊ</option>
<option value="230" class="cidades">IÇANÃ</option>
<option value="231" class="cidades">IPIXUNA</option>
<option value="232" class="cidades">IRANDUBA</option>
<option value="233" class="cidades">ITACOATIARA</option>
<option value="234" class="cidades">ITAMARATI</option>
<option value="235" class="cidades">ITAPIRANGA</option>
<option value="236" class="cidades">JAPURÁ</option>
<option value="237" class="cidades">JURUÁ</option>
<option value="238" class="cidades">JUTAÍ</option>
<option value="239" class="cidades">LÁBREA</option>
<option value="240" class="cidades">LAGO PRETO</option>
<option value="241" class="cidades">MANACAPURU</option>
<option value="242" class="cidades">MANAQUIRI</option>
<option value="243" class="cidades">MANAUS</option>
<option value="244" class="cidades">MANICORÉ</option>
<option value="245" class="cidades">MARAÃ</option>
<option value="246" class="cidades">MASSAUARI</option>
<option value="247" class="cidades">MAUÉS</option>
<option value="248" class="cidades">MOCAMBO</option>
<option value="249" class="cidades">MOURA</option>
<option value="250" class="cidades">MURUTINGA</option>
<option value="251" class="cidades">NHAMUNDÁ</option>
<option value="252" class="cidades">NOVA OLINDA DO NORTE</option>
<option value="253" class="cidades">NOVO AIRÃO</option>
<option value="254" class="cidades">NOVO ARIPUANÃ</option>
<option value="255" class="cidades">OSÓRIO DA FONSECA</option>
<option value="256" class="cidades">PARINTINS</option>
<option value="257" class="cidades">PAUINI</option>
<option value="258" class="cidades">PEDRAS</option>
<option value="259" class="cidades">PRESIDENTE FIGUEIREDO</option>
<option value="260" class="cidades">REPARTIMENTO</option>
<option value="261" class="cidades">RIO PRETO DA EVA</option>
<option value="262" class="cidades">SANTA ISABEL DO RIO NEGRO</option>
<option value="263" class="cidades">SANTA RITA</option>
<option value="264" class="cidades">SANTO ANTÔNIO DO IÇÁ</option>
<option value="265" class="cidades">SÃO FELIPE</option>
<option value="266" class="cidades">SÃO GABRIEL DA CACHOEIRA</option>
<option value="267" class="cidades">SÃO PAULO DE OLIVENÇA</option>
<option value="268" class="cidades">SÃO SEBASTIÃO DO UATUMÃ</option>
<option value="269" class="cidades">SILVES</option>
<option value="270" class="cidades">TABATINGA</option>
<option value="271" class="cidades">TAPAUÁ</option>
<option value="272" class="cidades">TEFÉ</option>
<option value="273" class="cidades">TONANTINS</option>
<option value="274" class="cidades">UARINI</option>
<option value="275" class="cidades">URUCARÁ</option>
<option value="276" class="cidades">URUCURITUBA</option>
<option value="277" class="cidades">VILA PITINGA</option>
You can display the HTML and explain what comes in the ajax string?
– Sergio
Forgive me Sergio. HTML would be what exactly? Can I put the PHP that brings select (HTML), because it is within a method. That would be it?
– user24136
Yes can be php, better still would be the string that returns ajax. Type
console.log(res);
within thatsuccess: function(res) {
. But also the HTML of#cidades
– Sergio
Right. I’ll edit my post and put PHP there...
– user24136
I don’t know if that’s exactly what it would be, see: Calling Element.createShadowRoot() for an element which already hosts a shadow root is deprecated. See https://www.chromestatus.com/features/4668884095336448 for more Details. VM12894:5 Uncaught Typeerror: Cannot read Property 'removeAttribute' of null(Anonymous Function) @ VM12894:5(Anonymous Function) @ VM12894:20 edit-schools.php? key=1679091...: 431 and the other Combox
– user24136
"and the other" - that’s what was interesting to see. Which I assume is on the console after you’ve done
console.log(res);
– Sergio
Right. I edited my post... because it was too long to put here
– user24136
How are you getting the
$id
you switch to PHP function?– Sergio
The problem is there.... when I edit, the city field is disabled and is only enabled when I select a state. It would need it to be disabled and with the selected city without having to select a state first...
– user24136
But in php you have
public function listarCidades($id){
and I imagine it’s theid
coming from Ajax. How are you receiving this value in PHP and calling this function?– Sergio
Actually this id comes from the link edit-data.php? key=(id that comes from the database) and on the edit page, picked up with $_REQUEST. The link that displays cities is City View.php? id=+id; which is inside jquery
– user24136
It’s the line where to do something like
$id = $_REQUEST['id'];
and then how do you treat this variable until you call the function that is interesting for the question...– Sergio
I call it include("classes/methodsClass.php"); $methods = new methodsClass(); $methods->listCities($id);
– user24136
And you use
$id = $_REQUEST['id'];
like this? with nothing else?– Sergio
that... caught and play within the method, because it comes from a query string
– user24136
Okay, now there are even more questions before you realize your complete logic :) Within the function
listarCidades
you are generating this HTML:<select name=\"Estados\" class=\"select1_si ... etc
. That’s for the states... right? And where does the HTML that the console showed?<option value="1" class="cidades">ACRELÂNDIA</option>
etc...
?– Sergio
After calling the function, in another div I put the call of the cities like this: <span class="loading">Wait, loading...</span> <select name="Cities" class="select2_single form-control" id="cities"> </select> and below the post jquery $("#states"). change(Function() {.....
– user24136