Gracias
Código PHP:
<?php require_once('../Connections/conexion.php'); ?>
<?php include('../lang/lang.php'); ?>
<?php include('../inc/config.php'); ?>
<?php if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; }
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break; }
return $theValue; } }
mysql_select_db($database_conexion, $conexion);
$query_pais = "SELECT * FROM countries ORDER BY name ASC";
$pais = mysql_query($query_pais, $conexion) or die(mysql_error());
$row_pais = mysql_fetch_assoc($pais);
$totalRows_pais = mysql_num_rows($pais);
$colname_estado = "-1";
if (isset($_POST['id'])) {
$colname_estado = $_POST['id']; }
mysql_select_db($database_conexion, $conexion);
$query_estado = sprintf("SELECT * FROM states WHERE country_id = %s ORDER BY name ASC", GetSQLValueString($colname_estado, "int"));
$estado = mysql_query($query_estado, $conexion) or die(mysql_error());
$row_estado = mysql_fetch_assoc($estado);
$totalRows_estado = mysql_num_rows($estado);
$colname_ciudad = "-1";
if (isset($_POST['id'])) {
$colname_ciudad = $_POST['id']; }
mysql_select_db($database_conexion, $conexion);
$query_ciudad = sprintf("SELECT * FROM cities WHERE state_id = %s ORDER BY name ASC", GetSQLValueString($colname_ciudad, "int"));
$ciudad = mysql_query($query_ciudad, $conexion) or die(mysql_error());
$row_ciudad = mysql_fetch_assoc($ciudad);
$totalRows_ciudad = mysql_num_rows($ciudad); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<head>
<meta http-equiv="Content-Type" content="text/html; />
<title></title>
<script type="text/javascript" src="../js/combo_dependientes.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<select name="country" class="countries" id="countryId">
<option value="">Select Country</option>
<option value="0" selected="selected"><?php echo $TEXT['acceso-co_error']; ?></option>
<?php do { ?>
<option value="<?php echo $row_pais['id']?>"><?php echo $row_pais['name']?></option>
<?php } while ($row_pais = mysql_fetch_assoc($pais));
$rows = mysql_num_rows($pais);
if($rows > 0) {
mysql_data_seek($pais, 0);
$row_pais = mysql_fetch_assoc($pais); } ?>
</select>
<select name="state" class="states" id="stateId">
<option value="">Select State</option>
<option value="0" selected="selected"><?php echo $TEXT['acceso-st_error']; ?></option> <?php do {?>
<option value="<?php echo $row_estado['id']; ?>"><?php echo $row_estado['name']; ?></option>
<?php } while ($row_estado = mysql_fetch_assoc($estado));
$rows = mysql_num_rows($estado);
if($rows > 0) {
mysql_data_seek($estado, 0);
$row_estado = mysql_fetch_assoc($estado);} ?>
</select>
<select name="city" class="cities" id="cityId">
<option value="">Select City</option>
<option value="0" selected="selected"><?php echo $TEXT['acceso-ci_error']; ?></option> <?php do {?>
<option value="<?php echo $row_ciudad['id']?>"><?php echo $row_ciudad['name']?></option>
<?php } while ($row_ciudad = mysql_fetch_assoc($ciudad));
$rows = mysql_num_rows($ciudad);
if($rows > 0) {
mysql_data_seek($ciudad, 0);
$row_ciudad = mysql_fetch_assoc($ciudad);} ?>
</select>
<?php foreach($_POST as $nombre_campo => $valor){
$asignacion = "\$" . $nombre_campo . "='" . $valor . "';";
eval($asignacion); } ?>
</body>
</html>
<?php mysql_free_result($pais);
mysql_free_result($estado);
mysql_free_result($ciudad);?>
Código MySQL:
Ver original
Código MySQL:
Ver original
Código MySQL:
Ver original
combo_dependientes.js
Código Javascript:
Ver original
function ajaxCall() { this.send = function(data, url, method, success, type) { type = type||'json'; var successRes = function(data) { success(data); } var errorRes = function(e) { console.log(e); alert("Error found \nError Code: "+e.status+" \nError Message: "+e.statusText); $('#loader').modal('hide'); } $.ajax({ url: url, type: method, data: data, success: successRes, error: errorRes, dataType: type, timeout: 60000 }); } } function locationInfo() { var rootUrl = "http://lab.iamrohit.in/php_ajax_country_state_city_dropdown/api.php"; var call = new ajaxCall(); this.getCities = function(id) { $(".cities option:gt(0)").remove(); var url = rootUrl+'?type=getCities&stateId=' + id; var method = "post"; var data = {}; $('.cities').find("option:eq(0)").html("Please wait.."); call.send(data, url, method, function(data) { $('.cities').find("option:eq(0)").html("Select City"); if(data.tp == 1){ $.each(data['result'], function(key, val) { var option = $('<option />'); option.attr('value', val).text(val); option.attr('cityid', key); $('.cities').append(option); }); $(".cities").prop("disabled",false); } else{ alert(data.msg); } }); }; this.getStates = function(id) { $(".states option:gt(0)").remove(); $(".cities option:gt(0)").remove(); var url = rootUrl+'?type=getStates&countryId=' + id; var method = "post"; var data = {}; $('.states').find("option:eq(0)").html("Please wait.."); call.send(data, url, method, function(data) { $('.states').find("option:eq(0)").html("Select State"); if(data.tp == 1){ $.each(data['result'], function(key, val) { var option = $('<option />'); option.attr('value', val).text(val); option.attr('stateid', key); $('.states').append(option); }); $(".states").prop("disabled",false); } else{ alert(data.msg); } }); }; this.getCountries = function() { var url = rootUrl+'?type=getCountries'; var method = "post"; var data = {}; $('.countries').find("option:eq(0)").html("Please wait.."); call.send(data, url, method, function(data) { $('.countries').find("option:eq(0)").html("Select Country"); console.log(data); if(data.tp == 1){ $.each(data['result'], function(key, val) { var option = $('<option />'); option.attr('value', val).text(val); option.attr('countryid', key); $('.countries').append(option); }); $(".countries").prop("disabled",false); } else{ alert(data.msg); } }); }; } $(function() { var loc = new locationInfo(); loc.getCountries(); $(".countries").on("change", function(ev) { var countryId = $("option:selected", this).attr('countryid'); if(countryId != ''){ loc.getStates(countryId); } else{ $(".states option:gt(0)").remove(); } }); $(".states").on("change", function(ev) { var stateId = $("option:selected", this).attr('stateid'); if(stateId != ''){ loc.getCities(stateId); } else{ $(".cities option:gt(0)").remove(); } }); });