AngularJS ile aranılan kelimeyi sorgulama
İlk önce ara.htm veya ara.php sayfası oluşturalım.
<!DOCTYPE html>
<html ng-app>
<head>
<title>AngularJS ile sorgulama</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"/>
<script src="http://code.angularjs.org/angular-1.0.0.min.js"></script>
<script src="ara.js"></script>
</head>
<body>
<div ng-controller="AraCtrl">
<form class="well form-search">
<label>Ara:</label>
<input type="text" ng-model="kelimeler" class="input-medium search-query" placeholder="kelime girin...">
<button type="submit" class="btn" ng-click="sorgula()">Şehir Ara</button>
<p class="help-block">"Adana" veya "Burdur"</p>
</form>
<pre ng-model="result">
{{result}}
</pre>
</div>
</body>
</html>
Daha sonra ara.js sayfası oluştralım.
function AraCtrl($scope, $http) {
$scope.url = 'ara.php';
$scope.sorgula= function() {
$http.post($scope.url, { "data" : $scope.keywords}).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
$scope.result = data;
})
.
error(function(data, status) {
$scope.data = data || "Hata Olustu...";
$scope.status = status;
});
};
}
Son olarak da ara.php sayfası oluşturalım.
<?PHP include ("db.php");
// $_POST veya $_GET kullanılmıyor!
$veri = file_get_contents("php://input");
$objData = json_decode($veri);
$q = 'SELECT * FROM `iller` WHERE sehir LIKE "%'.$objData->data.'%"';
$res = $mysqli->query($q);
//echo $q;
$liste='';
if ( $res->num_rows > 0 ){
while($nt = $res->fetch_array()) {
$liste .= $nt['sehir'].', ';
}
echo substr($liste, 0, -2);
} else {
echo 'Kayıt bulunamadı....';
}
?>
