RoamEase/indexes/user/getCities.php
2025-06-06 17:31:03 +08:00

28 lines
680 B
PHP

<?php
// Include your database connection script
include '../connectDB.php';
// Get the state_id from the query string
$stateId = isset($_GET['state_id']) ? intval($_GET['state_id']) : 0;
// Array to hold cities
$cities = [];
// Fetch cities from the database based on state_id
if ($stateId > 0) {
$query = "SELECT `id`, `name` FROM cities WHERE state_id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $stateId);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$cities[] = $row;
}
}
// Return the cities as JSON
header('Content-Type: application/json');
echo json_encode($cities);
?>