My journey with the Symfony2 PHP framework is just beginning, and I am aware that it follows the MVC pattern for backend development. One of my current tasks involves creating a form that stores data in a database and then sends an email to the user with the information they inputted. To achieve this, I have utilized Doctrine, Swiftmailer, and entities within the Symfony2 framework.
When I use '<form method='post' action='saveIndex'></form>', everything functions as expected. However, when attempting to implement AngularJS AJAX for posting data, I encounter a 'Notice: Undefined index' error.
Here is the HTML code from my 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>
Below is the code snippet from 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
});
}
}
]);
Here is the Symfony2 controller code:
// Save Information
public function saveIndexAction(){
$post = new Post();
$request = $this->get('request');
$params = $request->request->all();
$session = $this->get('session');
$this->saveIndex(
$params['productId'],
$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=json_encode($post);//json encode the array
return new Response($return,200,array('Content-Type'=>'application/json'));
}
I'm seeking guidance on how to successfully perform an AJAX POST request with Symfony2 controller and receive the response back in AngularJS for client-side routing. Any assistance would be greatly appreciated. Thank you.