我与jquery related selects pugin吵架。我使用.php
方法获取json
文件获取数据,如下所示:
<?php
// simulate that this proccess might take a while so you can see the loadingMessage option work.
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";
$villages = array();
$villages['MA']['BARN']['CHA']['CCHA'] = 'Chatham';
$villages['MA']['BARN']['CHA']['SCHA'] = 'South Chatham';
$villages['MA']['BARN']['CHA']['NCHA'] = 'North Chatham';
$villages['MA']['BARN']['CHA']['WCHA'] = 'West Chatham';
$villages['MA']['BARN']['CHA']['CHAP'] = 'Chatham Port';
$villages['MA']['BARN']['DEN']['CDEN'] = 'Dennis';
$villages['MA']['BARN']['DEN']['SDEN'] = 'South Dennis';
$villages['MA']['BARN']['DEN']['WDEN'] = 'West Dennis';
$villages['MA']['BARN']['DEN']['EDEN'] = 'East Dennis';
$villages['MA']['BARN']['DEN']['DENP'] = 'Dennis Port';
$villages['MA']['BARN']['YAR']['CYAR'] = 'Yarmouth';
$villages['MA']['BARN']['YAR']['SYAR'] = 'South Yarmouth';
$villages['MA']['BARN']['YAR']['WYAR'] = 'West Yarmouth';
$villages['MA']['BARN']['YAR']['BASS'] = 'Bass River';
$villages['MA']['BARN']['YAR']['YPOR'] = 'Yarmouth Port';
$villages['MA']['PLYM']['BRI']['STAN'] = 'Stanley';
$villages['MA']['PLYM']['BRI']['SCOT'] = 'Scotland';
$villages['MA']['PLYM']['BRI']['PMVI'] = 'Paper Mill Village';
$villages['MA']['PLYM']['BRI']['SBRI'] = 'South Bridgewater';
$villages['MA']['PLYM']['MAR']['ROCK'] = 'Brant Rock';
$villages['MA']['PLYM']['MAR']['GHAR'] = 'Green Harbor';
$villages['MA']['PLYM']['MAR']['HILL'] = 'Marshfield Hills';
$villages['MA']['PLYM']['MAR']['BLUF'] = 'Ocean Bluff';
$villages['MA']['PLYM']['WAR']['ONSE'] = 'Onset';
$villages['MA']['PLYM']['WAR']['WCEN'] = 'Wareham Center';
$villages['MA']['PLYM']['WAR']['WESW'] = 'West Wareham';
$villages['MA']['PLYM']['WAR']['WEWN'] = 'Weweantic';
$villages['VT']['CHIT']['BUR']['BURL'] = 'Burlington';
$villages['VT']['CHIT']['BUR']['SBUR'] = 'South Burlington';
$villages['VT']['CHIT']['ESS']['ESSE'] = 'Essex';
$villages['VT']['CHIT']['ESS']['ESSJ'] = 'Essex Junction';
$villages['VT']['CHIT']['ESS']['JERI'] = 'Jerico';
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 '{}';
}
?>
这项工作,但当我使用此代码检查PHP
错误时:
error_reporting(E_ALL); ini_set('display_errors', '1');
我看到了这个错误:
Notice: Undefined index: stateID in C:\xampp\htdocs\test\datasupplier.php on line 6
Notice: Undefined index: countyID in C:\xampp\htdocs\test\datasupplier.php on line 7
Notice: Undefined index: townID in C:\xampp\htdocs\test\datasupplier.php on line 8
Notice: Undefined index: html in C:\xampp\htdocs\test\datasupplier.php on line 9
我该如何解决此错误?!
注意:我现在可以修复error_reporting(E_ERROR | E_WARNING | E_PARSE);
并隐藏错误。
答案 0 :(得分:1)
将isset()
与ternary operator
$stateID = (isset($_GET['stateID'])) ? $_GET['stateID'] : "";
$countyID = (isset($_GET['countyID'])) ? $_GET['countyID'] : "";
$townID = (isset($_GET['townID'])) ? $_GET['townID'] : "";
$html = (isset($_GET['html'])) ? $_GET['html'] : "";