The admin side manages Country, State and City data used by the three linked dropdowns. State-changing operations should use POST, validate parent IDs, and use prepared INSERT/DELETE queries.
$.post(
"admin-add-country.php",
{
name:
$( "#country_name" )
.val()
.trim()
},
null,
"json"
);
$.post(
"admin-add-state.php",
{
country_id:
$( "#country" ).val(),
name:
$( "#state_name" )
.val()
.trim()
},
null,
"json"
);
$.post(
"admin-add-city.php",
{
state_id:
$( "#state" ).val(),
name:
$( "#city_name" )
.val()
.trim()
},
null,
"json"
);
$stmt = $dbo->prepare(
'DELETE FROM city
WHERE id = :id'
);
$stmt->execute([
'id' => $id
]);
See SQL DELETE and PDO connection.
<?Php
require "config.php";// connection details
error_reporting(0);//no error reporting
///////////////
$country_code=$_POST['country_code'];// Country code
$country=$_POST['country'];
///////////// Validate the inputs ////////////
if(strlen($country_code) > 0 and strlen($country)>0){
$sql=$dbo->prepare("insert into plus2_country (country,country_code) values(:country,:country_code)");
$sql->bindParam(':country',$country,PDO::PARAM_STR,25);
$sql->bindParam(':country_code',$country_code,PDO::PARAM_STR,4);
if($sql->execute()){
$state_id=$dbo->lastInsertId();
$msg= " Country Added : $country : code = $country_code ";
}
else{
$msg = " Database Problem Not able to add data please contact Admin ";
}
$("#f1")[0].reset();
}else{
$msg=" Enter Country and Country Code";
}
echo $msg;
?><script>
$(document).ready(function() {
///////////////////////////
$("#b1").click(function(){
$("#msg").html('Just Wait ...');
$("#msg").show();
$.post( "dropdown3-admin-add-countryck.php", {"country_code":$('#country_code').val(),"country":$('#country').val()},function(return_data,status){
$("#msg").html(return_data.msg);
setTimeout(function() { $("#msg").hide(); }, 2000);
if(return_data.db_status=='OK'){
window.location = "dropdown3-admin-add-country.php";
}
},'json');
});
////////////////////////////
})
</script>if(strlen($country_code) > 0 and strlen($state)>0){
$sql=$dbo->prepare("insert into plus2_state (state,country_code) values(:state,:country_code)");
$sql->bindParam(':state',$state,PDO::PARAM_STR,25);
$sql->bindParam(':country_code',$country_code,PDO::PARAM_STR,4);
if($sql->execute()){
$state_id=$dbo->lastInsertId();
$msg= " State Added : $state : State_id = $state_id";
}
else{
$msg = " Database Problem Not able to add data please contact Admin ";
}
}else{
$msg=" Select Country and enter state name";
}
echo $msg;<script>
$(document).ready(function() {
$("#b1").prop('disabled',true);
$("#t1").prop("disabled", true);
////////////////////////////////////
$("#country_code").change(function(){ // change function of listbox
$("#b1").prop('disabled',false);
$("#t1").prop("disabled",false);
});
///////////////////////////
////////////////////////////////////
///////////////////////////
$("#b1").click(function(){
$("#msg").html('Just Wait ...');
$("#msg").show();
$.post( "dropdown3-admin-add-stateck.php", {"country_code":$('#country_code').val(),"state":$('#t1').val()},function(return_data,status){
$("#f1")[0].reset();
$("#msg").html(return_data);
setTimeout(function() { $("#msg").hide(); }, 2000);
});
});
////////////////////////////
})
</script>if(strlen($state_id) > 0 and strlen($city) > 1){
$sql=$dbo->prepare("insert into plus2_city (city,state_id) values(:city,:state_id)");
$sql->bindParam(':city',$city,PDO::PARAM_STR,25);
$sql->bindParam(':state_id',$state_id,PDO::PARAM_INT,4);
if($sql->execute()){
$city_id=$dbo->lastInsertId();
$msg= " City Added : $city : id = $city_id ";
}
else{
$msg = " Database Problem Not able to add data please contact Admin ";
}
}else{
$msg=" Enter all details ";
}
echo $msg;<script>
$(document).ready(function() {
$("#b1").prop('disabled',true);
$("#t1").prop("disabled", true);
////////////////////////////////////
$("#country_code").change(function(){//change function of listbox
$("#state_id,#city_id").empty();
$("#b1").prop('disabled',true);
$.post( "dropdown3ck.php", {"country_code":
$('#country_code').val()},function(return_data,status){
$("#state_id").append("<option value=''>Select One</option>");
$.each(return_data.state, function(key,value){
$("#state_id").append("<option value=" + value.state_id +">"
+value.state_id + ':' + value.state+"</option>");
});
},"json");
});
///////////////////////////
////////////////////////////////////
$("#state_id").change(function(){//change function of listbox
if ($( "#country_code" ).val().length >0 &&
$( "#state_id" ).val().length >0 ){
$("#b1").prop('disabled',false);
$("#t1").prop("disabled", false);
}
});
///////////////////////////
$("#b1").click(function(){
$("#msg").html('Just Wait ...');
$("#msg").show();
$.post( "dropdown3-admin-add-cityck.php",
{"state_id":$('#state_id').val(),"city":$('#t1').val()},
function(return_data,status){
$("#msg").html(return_data);
setTimeout(function() { $("#msg").hide(); }, 2000);
$("#f1")[0].reset();
$("#t1").attr('value','');
});
});
////////////////////////////
})
</script>DELETE FROM plus2_city WHERE city_id=:city_idDELETE FROM plus2_state,plus2_city using plus2_state
INNER JOIN plus2_city
WHERE plus2_state.state_id=plus2_city.state_id
AND plus2_state.state_id=:state_idDELETE FROM plus2_country,plus2_state,plus2_city USING plus2_state
INNER JOIN plus2_city
INNER JOIN plus2_country
WHERE plus2_country.country_code=plus2_state.country_code
AND plus2_state.state_id=plus2_city.state_id
AND plus2_country.country_code=:country_code
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.