0
我有這個PHP文件的國家/州/鎮使用json方法列表。從php json文件檢索名稱ID
PHP文件:
sleep(1);
$stateID = $_GET['stateID'];
$countyID = $_GET['countyID'];
$townID = $_GET['townID'];
$html = $_GET['html'];
$states = array();
$states['MA'] = "Massachusetts";
$states['VT'] = "Vermont";
$states['SC'] = "South Carolina";
$counties = array();
$counties['MA']['BARN'] = 'Barnstable';
$counties['MA']['PLYM'] = 'Plymouth';
$counties['VT']['CHIT'] = 'Chittenden';
$counties['SC']['ANDE'] = 'Anderson';
$towns = array();
$towns['MA']['BARN']['CHA'] = "Chatham";
$towns['MA']['BARN']['DEN'] = "Dennis";
$towns['MA']['BARN']['YAR'] = "Yarmouth";
$towns['MA']['PLYM']['BRI'] = "Bridgewater";
$towns['MA']['PLYM']['MAR'] = "Marshfield";
$towns['MA']['PLYM']['WAR'] = "Wareham";
$towns['VT']['CHIT']['BUR'] = "Burlington";
$towns['VT']['CHIT']['ESS'] = "Essex";
if($stateID && !$countyID && !$townID){
echo json_encode($counties[$stateID]);
} elseif($stateID && $countyID && !$townID) {
echo json_encode($towns[$stateID][$countyID]);
} elseif(isset($villages[$stateID][$countyID][$townID])) {
echo json_encode($villages[$stateID][$countyID][$townID]);
} else {
echo '{}';
}
我檢索MySql數據庫值(例如:MA的國家)。 現在我需要打印國家/州/城鎮名稱到選擇框下拉這樣。
<select id="country" class="validate[required]" name="country">
<option value="0">Choose ...</option>
<option selected="selected" value="'.$country['selector'].'">Show Country Name From php File</option>
</select>
<select id="state" class="validate[required]" name="state">
<option value="0">Choose ...</option>
<option selected="selected" value="'.$state['selector'].'">Show state Name From php File</option>
</select>
<select id="town" class="validate[required]" name="town">
<option value="0">Choose ...</option>
<option selected="selected" value="'.$town['selector'].'">Show town Name From php File</option>
</select>
我該如何打印?
'<選項選擇= 「選擇」 值= 「 '$國家[ '選擇'。 ' 」>「'。$國家[ '選擇'。'」' –
你想填充盒服務器端還是使用AJAX? JSON(甚至MySQL)的使用讓我感到困惑。 – eggyal
在選項標籤之間打印相同的值也 –