Forum Moderators: open
<!DOCTYPE html>
<html>
<head>
<title>State and City Redirect</title>
</head>
<body>
<form id="locationForm">
<label for="state">State:</label>
<select id="state" name="state">
<option value="">Select a State</option>
<!-- state options -->
<option value="California">California</option>
<option value="Texas">Texas</option>
<!-- ... other states ... -->
</select>
<label for="city">City:</label>
<select id="city" name="city" disabled>
<option value="">Select a City</option>
<!-- Cities will be loaded based on state selection -->
</select>
</form>
<script src="redirect.js"></script>
</body>
</html>
document.getElementById('state').addEventListener('change', function() {
var state = this.value;
var citySelect = document.getElementById('city');
citySelect.innerHTML = '<option value="">Select a City</option>'; // Reset city dropdown
citySelect.disabled = !state;
if (state) {
fetch(state + '.json') // Fetch the JSON file for the selected state
.then(response => response.json())
.then(data => {
data.forEach(function(city) {
var option = document.createElement('option');
option.value = city.url;
option.textContent = city.name;
citySelect.appendChild(option);
});
});
}
});
document.getElementById('city').addEventListener('change', function() {
var url = this.value;
if (url) {
window.location.href = url; // Redirect to the selected city's URL
}
});
[
{ "name": "Los Angeles", "url": "https://example.com/los-angeles" },
{ "name": "San Francisco", "url": "https://example.com/san-francisco" },
// ... other cities ...
]
[
{ "name": "Dallas", "url": "https://example.com/Dallas" },
{ "name": "San Antonio", "url": "https://example.com/San Antonio" },
// ... other cities ...
]