Hi,
I have a script that posts my form data in a database. In this form are two dates, a startdate and an enddate. The script works perfect. But there is also a ‘hours’ field that should be filled by the amount of days times 8. If the startdate is 27-11-2014, and the enddate is 28-11-2014, the script should recognize that this are 2 days and writes 16 hours in the ‘hours’ column.
Controller
$scope.addNewRequest = function(){
var data = {
"employeeid": LoggedOnProfile[0].employeeid,
"startdate": $scope.requestData.startdate,
"enddate": $scope.requestData.enddate,
"type": $scope.requestData.type,
"reason": $scope.requestData.reason,
"notes": $scope.requestData.notes
};
console.log("inserted Successfully", data);
$http.post("http://url/to/insertscript.php", data,{"employeeid": LoggedOnProfile[0].employeeid, "startdate": $scope.requestData.startdate, "enddate": $scope.requestData.enddate, "type": $scope.requestData.type, "reason": $scope.requestData.reason, "notes": $scope.requestData.notes})
.success(function(data, status, headers, config){
console.log("inserted Successfully", data);
});
$scope.loadingIndicator = $ionicLoading.show({
content: 'Loading Data',
animation: 'fade-in',
showBackdrop: false,
maxWidth: 200,
showDelay: 100
});
$timeout(function() {
$scope.closeForm();
$scope.loadingIndicator.hide();
$scope.requestData.startdate = "";
$scope.requestData.enddate = "";
$scope.requestData.type = "";
$scope.requestData.reason = "";
$scope.requestData.notes = "";
}, 1000);
};
PHP script:
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;
$type = $vars->type;
$reason = $vars->reason;
$notes = $vars->notes;
$date = date('Y-m-d H:i');
$employeeid = $vars->employeeid;
$sql = "INSERT INTO Requests (employeeid, date, startdate, enddate, type, reason, notes, status, hours, comment)
VALUES ('$employeeid', '$date', '$startdate', '$enddate', '$type', '$reason', '$notes', 'Pending', '', '')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
Has someone an idea how to do this?
Thanks in advance,
Jan