Jump to content

combobox encadeadas


untz

Recommended Posts

boa tarde,

alguem pode-me dizer o que está mal aqui ? é que eu não percebo de jquery e o um amigo meu me enviou uns exemplos para ver como funcionava as combobox mas elas não aparecem.

o código é o seguinte:

index.php

<?php
$mysqli = new mysqli("localhost", "root", "", "jquery");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
<html>
<head>
<meta charset="ISO-8859-1">
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
$.get("get_countries.php", function(data, status) {
data = $.parseJSON(data);
if(data) {
$.each(data, function(index, element) {
 var option = $('<option />');
 option.attr('value', element.countries_id).text(element.country);
 $('#comboCountries').append(option);
});
$('#comboCountries').fadeIn();
}
});

$('#comboCountries').change(function(e) {
//se o item selecionado for o 0 não faz nada
if($(this)[0].selectedIndex!=0) {
var countries_id = $(this).val();
//limpar a comboRegions
$('#comboRegions').find('option').remove().end();
$.get("get_regions.php",
{
 countries_id: countries_id

},
function(data, status) {
 data = $.parseJSON(data);

 if(data!='0') {
 $.each(data, function(index, element) {
 var option = $('<option />');
 option.attr('value', element.regions_id).text(element.regions);
 $('#comboRegions').append(option);
 });
 $('#comboRegions').fadeIn();
 $('#comboRegions').trigger('change');
 }
 else {
 $('#comboCitys').hide();
 $('#comboRegions').hide();
 }
});

}
else {
//se não tivermos nada selecionado
$('#comboCitys').fadeOut();
$('#comboRegions').fadeOut();
}
});

$('#comboRegions').change(function(e) {
var regions_id = $(this).val();
$('#comboCitys').find('option').remove().end();
$.get("get_citys.php",
{
regions_id: regions_id
},
function(data, status) {
data = $.parseJSON(data);
if(data!='0') {
 $.each(data, function(index, element) {
 var option = $('<option />');
 option.attr('value', element.citys_id).text(element.citys);
 $('#comboCitys').append(option);
 });
 $('#comboCitys').fadeIn();
}
else {
 $('#comboCitys').hide();
}
});
});



});
</script>
</head>
<body>
<h1>Comboboxs encadeadas</h1>
<div id="myForm">
<form method="POST" name="firstForm">
<select id="comboCountries" style="display: none;">
<option value="0">Indique a país desejado</option>
</select>

<select id="comboRegions" style="display: none;"></select>
<select id="comboCitys" style="display: none;"></select>

</form>
</div>
<div id="msg"></div>
</body>
</html>

get_countries.php

<?php
$mysqli = new mysqli("localhost", "root", "", "jquery");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$countries = array();

if($stmt = $mysqli->prepare("Select * from countries order by country")) {
$stmt->execute();
$stmt->bind_result($countries_id, $country, $population, $flag, $is_active);

while ($stmt->fetch()) {

$array = [
'countries_id' => $countries_id,
'country' => $country,
'population' => $population,
'flag' => $flag,
'is_active' => $is_active
];

array_push($countries,$array);
}

$stmt->close();
}


if (count($countries)==0) die("0");
else die(json_encode($countries));
?>

get_regions.php

<?php
$mysqli = new mysqli("localhost", "root", "", "jquery");
$mysqli->set_charset('utf8');

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

//echo "id: $_GET[countries_id]";

$regionsArray = array();

if($stmt = $mysqli->prepare("Select * from regions where is_active=1 and countries_id=?")) {
$stmt->bind_param('i', $_GET['countries_id']);
$stmt->execute();
$stmt->bind_result($regions_id, $regions, $countries_id, $is_active);

while($stmt->fetch()) {

$array = [
'regions_id' => $regions_id,
'regions' => $regions,
'countries_id' => $countries_id,
'is_active' => $is_active
];
array_push($regionsArray, $array);
}

$stmt->close();
}
if (count($regionsArray)==0) die("0");
else die(json_encode($regionsArray));
?>

get_citys.php

<?php
$mysqli = new mysqli("localhost", "root", "", "jquery");
$mysqli->set_charset('utf8');

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$citysArray = array();

if($stmt = $mysqli->prepare("Select * from citys where is_active=1 and regions_id=?")) {
$stmt->bind_param('i', $_GET['regions_id']);
$stmt->execute();
$stmt->bind_result($citys_id, $citys, $regions_id, $is_active);

while($stmt->fetch()) {

$array = [
'citys_id' => $citys_id,
'citys' => $citys,
'regions_id' => $regions_id,
'is_active' => $is_active
];
array_push($citysArray, $array);
}

$stmt->close();
}
if (count($citysArray)==0) die("0");
else die(json_encode($citysArray));
?>

ficaria muito agradecido se alguem me ajudasse aqui sff=PHP

Edited by apocsantos
Link to post
Share on other sites

No final dos teus get_countries.php, get_xxx.php falta enviares o json:

echo json_encode($countries);

por exemplo.

EDIT: reparei agora que fazes die(json_encode em vez de echo... Nunca tentei desta maneira.

Podes colocar console.log para ver o que recebes?

function(data, status) {
console.log(data);
	 data = $.parseJSON(data);

Edited by apocsantos
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.