try the below slim framework 2 example with angularjs select box, it
has worked for me:
I have student name table from which i will show student names in select box, so make a table student or any your table:
my index.html file with ng-app is:
<html ng-app="mainApp">
<head>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript">
var app=angular.module('mainApp',[]);
angular.module('mainApp').controller('myCtrl1',function($scope,$http){
$http({
method : "GET",
url : "http://localhost/em/mod.php/devicegrp",
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function mySucces(response2) {
$scope.mydata2=response2.data;
},function myError(response2) {
$scope.mydata2=response.statusText;
});
});
</script>
</head>
<body ng-controller="myCtrl1">
<select class="form-control" ng-options="o.id as o.studentnm for o in mydata2" ng-model="valueselected" ></select>selected:{{valueselected}}
</body>
</html>
And my Slim framework 2 file is like:
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET,OPTIONS,POST,PUT,DELETE');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
require 'vendor/autoload.php';
$app->get('/get', function () use ($app) {
$sql = "select * from student";
try {
$db = getConnection();
$stmt = $db->query($sql);
$us = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($us);
}
catch(PDOException $e) {
echo '{"error":{"text":'.$e->getMessage() .'}}';
}
});
function getConnection() {
$dbhost="localhost";
$dbuser="root";
$dbpass="";
$dbname="restdb";
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
$app->run();
?>
Try like this