Hi,
I am using the following code to populate states based on country select using the php and jquery but everytime I select a country nothing is happening and my state dropdown is still empty.
where is the problem please? Happy New Year 🙂
Html:
<tr>
<td valign="middle">Country</td>
<td valign="middle">:</td>
<td valign="middle">
<select name="cboCountry" id="cboCountry" style="width: 100%" required>
<option value="">[Country..]</option>
<?php
$mysql_command = "CALL sp_populate_country()";
$mysql_query = $mysql_connection->prepare($mysql_command);
$mysql_query->execute();
while($mysql_row = $mysql_query->fetch())
{
?>
<option value="<?php echo $mysql_row['country_code_alpha2']; ?>" <?php if ($mysql_row['country_code_alpha2'] == $company_country) { echo 'selected'; } ?>><?php echo $mysql_row['country_name']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td valign="middle">State / City</td>
<td valign="middle">:</td>
<td valign="middle">
<select name="cboRegion" id="cboRegion" style="width: 100%">
<option value="">[State / City..]</option>
</select>
</td>
</tr>
Jquery:
<script type="text/javascript">
$("#cboCountry").change(function() {
// var country = $(this).find("option:selected").val();
// alert(country);
$.ajax({
url : "populate_region.php?id=" + $(this).val(),
type: 'GET',
dataType:'json',
cache: false,
success : function(data) {
if (data.success) {
$("#cboRegion").html(data.options);
}
else {
// Handle error
}
}
});
});
</script>
populate_region.php
<?php
include('../includes/php_header.php');
include($_SESSION["absolute_path"] . '/includes/connect2db.php');
$country_code = $_GET['id'];
if (!isset($country_code)) $reponse = array('success' => FALSE);
else
{
$mysql_command = "CALL sp_populate_region()";
$mysql_query = $mysql_connection->prepare($mysql_command);
$mysql_query->bindParam(':param_country_code', $country_code, PDO::PARAM_STR);
$mysql_query->execute();
while($mysql_row = $mysql_query->fetch())
{
$options .= '<option value="'. $mysql_row['region_id'] .'">'. $mysql_row['region_name'] .'</option>';
}
$response = array
(
'success' => TRUE,
'options' => $options
);
header('Content-Type: application/json');
echo json_encode($response);
// include($_SESSION["absolute_path"] . '/includes/php_footer.php');
}
?>