Table 1: state
CREATE TABLE `state` (
`stid` int(2) NOT NULL,
`stname` varchar(70) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `state` (`stid`, `stname`) VALUES
(1, 'Andhra Pradesh'),
(2, 'Arunachal Pradesh'),
(3, 'Assam'),
(4, 'Bihar'),
(24, 'Tamil Nadu'),
(36, 'Puducherry');
ALTER TABLE `state` ADD PRIMARY KEY (`stid`);
Table 2: city
CREATE TABLE `city` (
`cyid` int(10) NOT NULL,
`stateid` int(2) NOT NULL,
`cyname` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `city` (`cyid`, `stateid`, `cyname`) VALUES
(1, 24, 'Cbe');
ALTER TABLE `city` ADD PRIMARY KEY (`cyid`);
HTML/PHP Form:
<form method="post" action="" enctype="multipart/form-data">
State:
<?php
$sql=mysqli_query($conn,"SELECT stid,stname FROM state");
if(mysqli_num_rows($sql)){ ?>
<select class="form-control" name="stateid" id="stateid" required>
<option value="">Choose State</option>
<?php
while($rs=mysqli_fetch_array($sql)){ ?>
<option value="<?php echo $rs['stid']; ?>"><?php echo $rs['stname']; ?></option>
<?php }} ?>
</select>
<br><br>
City/Location:
<select class="form-control" name="cityid" id="cityid" required>
<option value="">Choose City/Location</option>
</select>
</form>
JavaScript:
<script type="text/javascript">
$(document).ready(function() {
$("#stateid").change(function() {
var stateid = $(this).val();
if(stateid != "") {
$.ajax({
url:"get-city.php",
data:{c_id:stateid},
type:'POST',
success:function(response) {
var resp = $.trim(response);
$("#cityid").html(resp);
}
});
}
});
});
</script>
Combined Code:
<?php
include("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#stateid").change(function() {
var stateid = $(this).val();
if(stateid != "") {
$.ajax({
url:"get-city.php",
data:{c_id:stateid},
type:'POST',
success:function(response) {
var resp = $.trim(response);
$("#cityid").html(resp);
}
});
}
});
});
</script>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
State:
<?php
$sql=mysqli_query($conn,"SELECT stid,stname FROM state where status='1'");
if(mysqli_num_rows($sql)){ ?>
<select class="form-control" name="stateid" id="stateid" required>
<option value="">Choose State</option>
<?php
while($rs=mysqli_fetch_array($sql)){ ?>
<option value="<?php echo $rs['stid']; ?>"><?php echo $rs['stname']; ?></option>
<?php }} ?>
</select>
<br><br>
City/Location:
<select class="form-control" name="cityid" id="cityid" required>
<option value="">Choose City/Location</option>
</select>
</form>
</body>
</html>
get-city.php
<?php
include("config.php");
if(isset($_POST['c_id'])) {
$sql = "select * from city where stateid=".mysqli_real_escape_string($conn, $_POST['c_id']);
$res = mysqli_query($conn, $sql);
if(mysqli_num_rows($res) > 0) {
echo "<option value=''>Choose City/Location</option>";
while($row = mysqli_fetch_object($res)) {
echo "<option value='".$row->cyid."'>".$row->cyname."</option>";
}
}
else
{
echo "<option value=''>Choose City/Location</option>";
}
} else {
header('location: ./');
}
?>
CREATE TABLE `state` (
`stid` int(2) NOT NULL,
`stname` varchar(70) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `state` (`stid`, `stname`) VALUES
(1, 'Andhra Pradesh'),
(2, 'Arunachal Pradesh'),
(3, 'Assam'),
(4, 'Bihar'),
(24, 'Tamil Nadu'),
(36, 'Puducherry');
ALTER TABLE `state` ADD PRIMARY KEY (`stid`);
Table 2: city
CREATE TABLE `city` (
`cyid` int(10) NOT NULL,
`stateid` int(2) NOT NULL,
`cyname` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `city` (`cyid`, `stateid`, `cyname`) VALUES
(1, 24, 'Cbe');
ALTER TABLE `city` ADD PRIMARY KEY (`cyid`);
HTML/PHP Form:
<form method="post" action="" enctype="multipart/form-data">
State:
<?php
$sql=mysqli_query($conn,"SELECT stid,stname FROM state");
if(mysqli_num_rows($sql)){ ?>
<select class="form-control" name="stateid" id="stateid" required>
<option value="">Choose State</option>
<?php
while($rs=mysqli_fetch_array($sql)){ ?>
<option value="<?php echo $rs['stid']; ?>"><?php echo $rs['stname']; ?></option>
<?php }} ?>
</select>
<br><br>
City/Location:
<select class="form-control" name="cityid" id="cityid" required>
<option value="">Choose City/Location</option>
</select>
</form>
JavaScript:
<script type="text/javascript">
$(document).ready(function() {
$("#stateid").change(function() {
var stateid = $(this).val();
if(stateid != "") {
$.ajax({
url:"get-city.php",
data:{c_id:stateid},
type:'POST',
success:function(response) {
var resp = $.trim(response);
$("#cityid").html(resp);
}
});
}
});
});
</script>
Combined Code:
<?php
include("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#stateid").change(function() {
var stateid = $(this).val();
if(stateid != "") {
$.ajax({
url:"get-city.php",
data:{c_id:stateid},
type:'POST',
success:function(response) {
var resp = $.trim(response);
$("#cityid").html(resp);
}
});
}
});
});
</script>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
State:
<?php
$sql=mysqli_query($conn,"SELECT stid,stname FROM state where status='1'");
if(mysqli_num_rows($sql)){ ?>
<select class="form-control" name="stateid" id="stateid" required>
<option value="">Choose State</option>
<?php
while($rs=mysqli_fetch_array($sql)){ ?>
<option value="<?php echo $rs['stid']; ?>"><?php echo $rs['stname']; ?></option>
<?php }} ?>
</select>
<br><br>
City/Location:
<select class="form-control" name="cityid" id="cityid" required>
<option value="">Choose City/Location</option>
</select>
</form>
</body>
</html>
get-city.php
<?php
include("config.php");
if(isset($_POST['c_id'])) {
$sql = "select * from city where stateid=".mysqli_real_escape_string($conn, $_POST['c_id']);
$res = mysqli_query($conn, $sql);
if(mysqli_num_rows($res) > 0) {
echo "<option value=''>Choose City/Location</option>";
while($row = mysqli_fetch_object($res)) {
echo "<option value='".$row->cyid."'>".$row->cyname."</option>";
}
}
else
{
echo "<option value=''>Choose City/Location</option>";
}
} else {
header('location: ./');
}
?>