我目前有一个HTML表单,其中包含两个选择标签,这些选择标签是通过php / json调用从MySQL数据库填充的。第一个选择包含一个位置/位置列表,根据用户选择的位置/位置,更新第二个选择(通过javascript)以列出该特定位置的教堂。我要解决的问题是,如果没有列出特定位置的教堂(第二个选择显示为空/无人居住),我想加载模式或定向到可以添加教堂的其他页面。我不知道从哪里开始!
下面是我用来首先填充位置选择下拉列表的javascript ...,当用户选择其他位置时,它将调用updateChurches()。
<?php
echo "var locations = $jsonLocations; \n";
echo "var churches = $jsonChurches; \n";
?>
function loadLocations(){
var select = document.getElementById("locationSelect");
select.onchange = updateChurches;
for(var i = 0; i < locations.length; i++){
select.options[i] = new Option(locations[i].val,locations[i].id);
}
}
function updateChurches(){
var churchSelect = this;
var churchid = this.value;
var churchSelect = document.getElementById("churchSelect");
churchSelect.options.length = 0; //delete all options if any present
for(var i = 0; i < churches[churchid].length; i++){
churchSelect.options[i] = new Option(churches[churchid][i].val,churches[churchid][i].id);
}
}
两个选择的HTML如下;
<label for="location">Location :</label>
<select name="location" class="form-control" id='locationSelect' required>
<label for="church">Church :</label>
<select name="church" class="form-control" id='churchSelect' required>