<?php
require 'db.php';
header('Content-Type: application/json');

if (isset($_GET['state_id'])) {
    $state_id = (int)$_GET['state_id'];
    $localities = [];
    
    $stmt = $mysqli1->prepare("SELECT id, name FROM localities WHERE state_id = ?");
    $stmt->bind_param("i", $state_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    while ($row = $result->fetch_assoc()) {
        $localities[] = $row;
    }
    
    echo json_encode($localities);
} else {
    echo json_encode([]);
}
?>