我是角色js的新手,我正在构建一个离子应用程序,其中,我试图填充select选项取决于第一个选择选项,但是首先选择的变量选择选项无法处理获取第二个选择选项
这是我的service.js
cityFunction: function(bookUser) {
var link = 'http://localhost/endpoints/citydetails.php';
return $http.post(link);
},
selectedsitesFunction: function(bookUser) {
var data = { selectedCity1: bookUser.selectedCity1};
var link = 'http://localhost/endpoints/selectedSites.php';
return $http.post(link, data);
}
controller.js
$scope.bookUser.city = city;
$scope.bookUser.selectedCity = "";
$scope.bookUser.selectedCity1 = "";
$scope.bookUser.site1 =[];
$scope.bookUser.selectedSite1 = "";
$scope.getOptions2 = function(bookUser){
$scope.bookUser.selectedCity1 = $scope.bookUser.selectedCity.Id;
AllServices.selectedsitesFunction(bookUser)
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
};
这里是html页面
<label class="item item-input item-select item-stacked-label">
<span class="input-label">City</span>
<select name="city" ng-model="bookUser.selectedCity" ng-options="item.City for item in bookUser.city" ng-change="getOptions2(bookUser)">
<option value="">-- select city --</option>
</select>
</label>
<label class="item item-input item-select item-stacked-label">
<span class="input-label">sites</span>
<select name="site1" ng-model="bookUser.selectedSite1" ng-options="option for option in bookUser.site1">
<option value="">--select site--</option>
</select>
</label>
这是.php代码
<?php
include 'connection.php';
$data = json_decode(file_get_contents("php://input"));
$selectedCity1 = $data->$selectedCity1;
$selectedSites = $db->query("SELECT Sites, Id from sitemaster where City = :selectedCity1 ");
$selectedsites = $selectedSites->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($selectedsites);
?>
每当我尝试访问此selectedCity1变量时,控制台都会显示错误
Notice: Undefined variable: selectedCity1 in C:\xampp\htdocs\endpoints\selectedSites.php on line 4
Fatal error: Cannot access empty property in C:\xampp\htdocs\endpoints\selectedSites.php on line 4
答案 0 :(得分:0)
<?php
include 'connection.php';
$data = json_decode(file_get_contents("php://input"));
$selectedCity1 = $_POST['selectedCity1'];
$query = mysql_query("select Sites, Id from sitemaster where City = "+$selectedCity1+"");
while ($fetch = mysql_fetch_array($query)) {
$output[] = array("site" => $fetch[0],"id" => $fetch[1];
}
echo json_encode($output);
?>
希望这可以帮到你:)谢谢
答案 1 :(得分:0)
<?php
include("connection.php");
$data = json_decode(file_get_contents("php://input"));
$selectedCity1 = $data->selectedCity1;
$selectedSites = $db->query("SELECT Id, Sites FROM sitemaster WHERE City ='$selectedCity1' ");
$selectedSites = $selectedSites->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($selectedSites);
?>