I want to improve the search for cities and states. I made this dynamic with Select2:
/*************** Procurar estado ***************/
$( ".estado" ).select2({
placeholder: "Estado",
ajax: {
url: "https://www.agenciamove.com.br/php/procurar_estados.php",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term // search term
};
},
processResults: function (data) {
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
return {
results: data
};
},
cache: true
}
});
/*************** Procurar estado ***************/
/*************** Procurar cidade ***************/
$( ".cidade" ).select2({
placeholder: "Cidade",
ajax: {
url: "https://www.agenciamove.com.br/php/procurar_cidades.php?estado=26",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term // search term
};
},
processResults: function (data) {
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
return {
results: data
};
},
cache: true
}
});
/*************** Procurar cidade ***************/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><linkhref="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script><selectclass="estado form-control" name="estado" id="estado" style="width: 50%";></select>
<select class="cidade form-control" name="cidade" id="cidade" style="width: 50%";></select>
In the part of looking for cities, it would be necessary to send the state id (as in the test I did "search_cidades.php? state = 26").
But I'm not finding a way for this to happen.
Is there a way, after looking for the state, to start the city with the state id?