I am a beginner in angular js and I am attempting to POST data to the server using an API that I have created:
function addmovie_post() {
{
$genre = $this->post('genre');
$cast = $this->post('cast');
$director = $this->post('director');
$title = $this->post('title');
$price = $this->post('price');
$image = $this->post('image');
}
{
//checking for empty parameters
if(empty($title) || empty($cast) || empty($genre) || empty($image) || empty($price) || empty($director))
{
$info->status = 'failure';
$info->error->code = 13;
$info->error->text = 'one or more parameters missing';
$this->response($info, 400);
}
}
{ // checking for duplicate record
$this->load->database();
$sql = 'select count(id) as records from movies where title = "'.$title.'" and cast = "'.$cast.'";';
$query = $this->db->query($sql);
$data = $query->row();
if ($data->records == "1") {
$info->status = 'failure';
$info->error->code = 18;
$info->error->text = 'duplicate record';
$this->response($info, 400);
}
}
$this->load->database();
$info = array('id'=> null, 'title'=>$title, 'cast'=>$cast,'genre'=>$genre, 'image'=>$image, 'price'=>$price,'director'=>$director );
$this ->db->insert('movies', $info);
$data->title = $title;
$data->cast = $cast;
$data->genre = $genre;
$data->price = $price;
$data->image = $image;
$data->director = $director;
$data->message = 'The movie has been added';
$this->response($data,201);
}
The API is functioning correctly. The code implementation with HTML in Angular seems to be error-free as no errors are displayed in the console. However, the data is not being sent through successfully and console.log does not show any data.
var app = angular.module('postapp', []);
app.controller('MyCtrl', function ($scope, $http) {
var formData = {
title: "default",
director: "default",
cast: "default",
genre: "default",
image: "default",
price: "default"
};
$scope.submitForm = function() {
$http({
url: "http://creative.coventry.ac.uk/~4078078/moviereviews/v1.0/index.php/movie/addmovie",
data: $scope.form,
method: 'POST',
headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
}).success(function(data){
console.log("OK", data)
}).error(function(error){"ERR", console.log($scope.error.error)})
};
});
HTML:
<div ng-app="postapp">
<form name="formData" ng-submit="submitForm()" ng-controller="MyCtrl">
title: <br/><input type="text" ng-model="form.title"> <br/><br/>
price: <br/><input type="text" ng-model="form.price"> <br/><br/>
genre: <br/><input type="text" ng-model="form.genre"> <br/><br/>
image: <br/><input type="text" ng-model="form.image"> <br/><br/>
cast: <br/><input type="text" ng-model="form.cast"> <br/><br/>
director: <br/><input type="text" ng-model="form.director"> <br/><br/>
</form>
</div>