Hi there,
I have an Ionic app with a form. This form consists of 2 fields, type=“date”. I also have a JSON file with several requests in it, these requests do all have a start- and enddate. I would like to send these two values (dates) to my JSON file which sends back the requests between those dates (and the requests with these dates too).
Form
<ion-view title="Search">
<ion-header-bar>
<h1 class="title">Request form</h1>
</ion-header-bar>
<ion-content>
<form ng-submit="searchReportsDates()" ng-controller="searchReportsCtrl">
<div class="list">
<div class="item item-divider">
Search between dates
</div>
<label class="item item-input">
<span class="input-label">Starts</span>
<input type="date" ng-model="searchData.startdate">
</label>
<label class="item item-input">
<span class="input-label">Ends</span>
<input type="date" ng-model="searchData.enddate">
</label>
<li class="item">
<button class="button button-width-red ion-checkmark"></button>
</li>
</div>
</form>
</ion-content>
</ion-view>
Controller
.controller('searchReportsCtrl', function($scope, $timeout, $http, $ionicLoading, RequestService) {
$scope.searchData = {};
$scope.searchReportsDates = function(){
var data = {
"startdate": $scope.searchData.startdate,
"enddate": $scope.searchData.enddate
};
$http.post("http://url/to/searchreports.php", data,{"startdate": $scope.searchData.startdate, "enddate": $scope.searchData.enddate})
.success(function(data, status, headers, config){
$scope.requestsBetweenDates = data;
console.log($scope.requestsBetweenDates); //this should contain the array with the requests
// or maybe like this?
//$scope.requestsBetweenDates = JSON.stringify(data);
});
$scope.loadingIndicator = $ionicLoading.show({
content: 'Loading Data',
animation: 'fade-in',
showBackdrop: false,
maxWidth: 200,
showDelay: 100
});
$timeout(function() {
$scope.loadingIndicator.hide();
$scope.searchData.startdate = "";
$scope.searchData.enddate = "";
}, 1000);
};
})
PHP script
<?php
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = file_get_contents('php://input');
$vars = json_decode($data);
$startdate = $vars->startdate;
$enddate = $vars->enddate;
$sql = "SELECT * FROM Requests, Users WHERE Requests.startdate=? AND Requests.enddate=?";
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $startdate);
$stmt->bindParam(2, $enddate);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); //array(0){}
if($result) {
foreach ($result as $row){
$return[]=array('employeeid'=>$row['employeeid'],
'picture'=>$row['picture'],
'firstname'=>$row['firstname'],
'lastname'=>$row['lastname'],
'role'=>$row['role'],
'id'=>$row['id'],
'startdate'=>$row['startdate'],
'enddate'=>$row['enddate'],
'type'=>$row['type'],
'reason'=>$row['reason'],
'notes'=>$row['notes'],
'level'=>$row['level']);
}
header('Content-type: application/json');
echo '' . json_encode($return) .'';
return true;
} else {
return false;
}
$conn = null;
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
?>
I’m guessing that i have to convert the strings, which I send to the PHP script, to actual dates… But the dates in the database should also be converted, right?
I think everything should work, except for the PHP script, right?
Thanks in advance,
Jan