Firstly I want to select State from the dropdown list and then based on the state that I selected, I want to display the cities in that state. How should I do that?
This is in addCat.php
<?php
$selectStates = $categoryObject->selectStates();
$selectCities = $categoryObject->selectCities($selectStates);
?>
<div class="x_content">
<br />
<form id="demo-form2" data-parsley-validate class="form-horizontal form-label-left" action="" method="POST">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">State Name <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select id="first-name" required="required" name="category" class="form-control col-md-7 col-xs-12">
<option value="">--Select--</option>
<?php
while($res = $selectStates->fetch_assoc())
{
?>
<option value="<?php echo $res["state_id"]; ?>"><?php echo $res["state_title"]; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">City Name <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select id="first-name" required="required" name="category" class="form-control col-md-7 col-xs-12">
<option value="">--Select--</option>
<?php
while($res = $selectCities->fetch_assoc())
{
?>
<option value="<?php echo $res["state_id"]; ?>"><?php echo $res["name"]; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<button type="submit" class="btn btn-app" name="addCategory"><i class="fa fa-save"></i> Save</button>
<button class="btn btn-app" type="reset"><i class="fa fa-repeat"></i> Reset</button>
</div>
</div>
</form>
</div>
This is in category.php
public function selectStates()
{
$states = "SELECT * FROM states";
return $this->db->select($states);
}
public function selectCities($states)
{
$cities = "SELECT * FROM city ORDER BY name ASC";
return $this->db->select($cities);
}
There are two tables cities and states. Cities have 'name' and 'state_id', while states have 'state_title' and 'state_id'
I am stuck on how to connect category.php and addCat.php as well as what code should I write as JS? I tried to find it on my own, But I am unable to use anything on my code. If someone can write that code for me, It would be really helpful. Please don't downvote the question. Not everyone is a pro-coder.
CodePudding user response:
You can with JS ajax onchange Example: https://www.tutsmake.com/country-state-city-database-in-mysql-php-ajax/