<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script language="javascript" type="text/javascript">
function showState(country_id)
{
document.frm.submit();
}
function showCity(state_id)
{
document.frm.submit();
}
</script>
<script type="text/javascript">
function countries()
{
var countryID = $('#country').val();
//alert(countryID);
$.ajax({
type:'POST',
url:'logic.php',
data:'country_id='+countryID,
success:function(html){
$('#state').html(html);
}
});
}
</script>
<script type="text/javascript">
function states()
{
var stateID = $('#state').val();
//alert(stateID);
$.ajax({
type:'POST',
url:'logic.php',
data:'state_id='+stateID,
success:function(html){
$('#city').html(html);
}
});
}
</script>
\t
select {
\t width: 50%;
\t padding: 12px 20px;
\t margin: 8px 0;
\t display: inline-block;
\t border: 1px solid #ccc;
\t border-radius: 2px;
\t box-sizing: border-box;
}
database :-
create 3 table countries , states , cities
--
-- Database: `crud`
-- Table structure for table `cities`
--
CREATE TABLE IF NOT EXISTS `cities` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`city_name` varchar(100) NOT NULL,
`state_id` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
--
-- Dumping data for table `cities`
--
INSERT INTO `cities` (`id`, `city_name`, `state_id`) VALUES
(1, 'bihar', '1'),
(2, 'xyz', '1'),
(3, 'indore', '2'),
(4, 'ujjain', '2'),
(5, 'xyz1', '3'),
(6, 'xyz2', '3');
-- --------------------------------------------------------
--
-- Table structure for table `countries`
--
CREATE TABLE IF NOT EXISTS `countries` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`country_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `countries`
--
INSERT INTO `countries` (`id`, `country_name`) VALUES
(1, 'india'),
(2, 'usa');
-- --------------------------------------------------------
--
-- Table structure for table `states`
--
CREATE TABLE IF NOT EXISTS `states` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`state_name` varchar(100) NOT NULL,
`country_id` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
--
-- Dumping data for table `states`
--
INSERT INTO `states` (`id`, `state_name`, `country_id`) VALUES
(1, 'up', '1'),
(2, 'mp', '1'),
(3, 'bihar', '1'),
(4, 'gujrat', '1'),
(5, 'washington', '2'),
(6, 'london', '2');
index.php
<!DOCTYPE html>
<html>
<head>
<h2> country state city dropdown using ajax,mysqli,php</h2>
</head>
<body>
\t \t
<h3 align="center" style="color:red;"> country state city dropdown using ajax ,mysqli,php </h3>
\t \t <form method="post" id="frm"> \t
\t <div class="form-group">
<label class="form-group">Country</label>
<?php
$sql3="select * from countries";
$sql_row3=mysqli_query($conn,$sql3);
?>
<select name="country" id="country" onchange="countries();">
\t <option value="">Select Country</option>
<?php
\t \t while($row_country=mysqli_fetch_assoc($sql_row3))
{
\t \t \t echo '<option value="'.$row_country['id'].'">'.$row_country['country_name'].'</option>';
\t \t
\t
\t }
?>
</select>
</div>
\t <div class="form-group">
\t \t \t \t \t <label class="form-group">State</label>
\t \t \t \t
<select name="state" id="state" onchange="states();">
\t <option value="">Select state</option>
\t
</select>
</div>
\t \t \t <div class="form-group"> <label class="form-group">City</label>
\t \t \t \t \t
<select name="city" id="city">
\t <option value="">Select city</option>
</select>
</div>
\t \t \t
\t \t </form>
\t
\t \t \t
</body>
</html>
logic.php
<?php
include("config.php");
if(isset($_POST['country_id']))
{
$country_id=($_POST['country_id']);
$sql1="select * from states where country_id='$country_id'";
$sql_row1=mysqli_query($conn,$sql1);
echo ' <option value="">Select state</option>';
while($row=mysqli_fetch_assoc($sql_row1)){
echo '<option value="'.$row['id'].'">'.$row['state_name'].'</option>';
}
}
//// select state
if(isset($_POST['state_id']))
{
$state_id=($_POST['state_id']);
$sql2="select * from cities where state_id='$state_id'";
$sql_row2=mysqli_query($conn,$sql2);
echo ' <option value="">Select city</option>';
while($rows=mysqli_fetch_assoc($sql_row2)){
echo '<option value="'.$rows['id'].'">'.$rows['city_name'].'</option>';
}
}
?>