I'm very new to symfony2 php framework but I know that its MVC for backend. I'm trying to create a form which get stored in the database and then an email is sent to the user with the same info that he/she typed in the form. To do that, I used doctrine, swiftmailer and entity in the symfony2 framework. When I use < form method='post' action='saveIndex' >< /form >, it works just fine but when I'm trying to implement angularjs's AJAX to post my data, I'm getting Notice: Undefined index error.
Html (in twig template):
<form class="productForm">
<div class="inputs" style="float: left">
<input type="text" name="productId" data-ng-model="signup.user.productId"/>
<input type="text" name="firstName" data-ng-model="signup.user.firstName"/>
<input type="text" name="lastName" data-ng-model="signup.user.lastName"/>
<input type="email" name="email" data-ng-model="signup.user.email"/>
<input type="text" name="price" data-ng-model="signup.user.price"/>
<input type="text" name="timeStamp" data-ng-model="signup.user.time"/>
<textarea name="comments" data-ng-model="signup.user.comments"></textarea>
<button type="submit" name="submit" class="btn btn-primary" ng- click="submit(signup.user)">Submit Request</button>
</div>
</div>
</form>
app.js:
myApp.controller('mainController',[
'$scope',
'$location',
'$http',
'$window',
'$rootScope',
function($scope, $location, $http, $window, $rootScope){
$scope.submit = function (user) {
$http({
method: 'POST',
url: 'saveIndex',
data: $scope.signup
})
.success(function(data, status){
$log.warn(data, status); //remove for production
$location.path('success');
})
.error(function(data, status){
$log.warn(data, status); //remove for production
});
}
}
]);
symfony2 controller:
// Save Information
public function saveIndexAction(){
$post = new Post(); // Call to entity named Post
$request = $this->get('request');
$params = $request->request->all();
$session = $this->get('session');
$this->saveIndex(
$params['productId'], <- GETTING UNDEFINED INDEX ERROR
$params['firstName'],
$params['lastName'],
$params['email'],
$params['price'],
$params['timeStamp'],
$params['comments'],
$session
);
$post->setProductId($params['productId']);
$post->setFirstName($params['firstName']);
$post->setLastName($params['lastName']);
$post->setEmail($params['email']);
$post->setPrice($params['price']);
$post->setTimeStamp($params['timeStamp']);
$post->setComments($params['comments']);
// Store information in the database
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
// Send the email
$this->mailAction
(
$post->getId(),
$params['productId'],
$params['firstName'],
$params['lastName'],
$params['email'],
$params['price'],
$params['timeStamp'],
$params['comments']
);
// Return the response
//return new Response('Info Submitted'.$post->getId());
$return=json_encode($post);//json encode the array
return new Response($return,200,array('Content-Type'=>'application/json'));//make sure it has the correct content type
}
I don't know how to make AJAX POST with with symfony 2 controller and get the response back to Angularjs to perform client side routing. Please any help is appreciated. Thank you.
signup.user.productId, but you are trying to access this in Symfony usingproductId. So that is where your issue is. if you do a var dump of the$paramsvariable in your controller, you will see how the array index is formed.Symfony\Component\HttpFoundation\JsonResponseobject instead of trying to create it yourself.