Manage Linked Dropdown Data

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.

Add a Country Top ↑

$.post(
    "admin-add-country.php",
    {
        name:
            $( "#country_name" )
                .val()
                .trim()
    },
    null,
    "json"
);

Add a State Top ↑

$.post(
    "admin-add-state.php",
    {
        country_id:
            $( "#country" ).val(),
        name:
            $( "#state_name" )
                .val()
                .trim()
    },
    null,
    "json"
);

Add a City Top ↑

$.post(
    "admin-add-city.php",
    {
        state_id:
            $( "#state" ).val(),
        name:
            $( "#city_name" )
                .val()
                .trim()
    },
    null,
    "json"
);

Delete Records Top ↑

$stmt = $dbo->prepare(
    'DELETE FROM city
     WHERE id = :id'
);

$stmt->execute([
    'id' => $id
]);

See SQL DELETE and PDO connection.

Original Admin Demos Top ↑

Download project

Protect admin pages with authentication/authorization and CSRF defenses before exposing them publicly.

Original Source Examples Retained for Migration Reference Top ↑

These source-page examples are retained so no learner-purpose example is silently lost. Use the modern examples above as the recommended implementation.

Original example 1

<?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;
?>

Original example 2

<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>

Original example 3

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;

Original example 4

<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>

Original example 5

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;

Original example 6

<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>

Original example 7

DELETE FROM plus2_city  WHERE city_id=:city_id

Original example 8

DELETE 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_id

Original example 9

DELETE  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

Additional Original Learning Routes Top ↑






plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer