0

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.

3
  • your variable in angular is something like signup.user.productId, but you are trying to access this in Symfony using productId. So that is where your issue is. if you do a var dump of the $params variable in your controller, you will see how the array index is formed. Commented Feb 24, 2014 at 20:13
  • improvement: return a Symfony\Component\HttpFoundation\JsonResponse object instead of trying to create it yourself. Commented Feb 24, 2014 at 22:47
  • @Sehael....THANKS ALOT for the var_dump...I was struggling for a way to debug my code (same as debugger; in JS). When I did that, found out the $params is getting an empty array. So, I included the following code: if (is_array($p)) { foreach($p as $key=>$val) { $params[$key]=$val; } } AND IT WORKED. thank you SO MUCH Commented Feb 25, 2014 at 4:27

1 Answer 1

1

So, upon var_dump($params), I found the output to be array (size = 0) undefined.

So, I included the following code:

protected function getRequestJson(){
    $params = null;
    $content = $this->get("request")->getContent();
    if (!empty($content))
    {
        $params = json_decode($content, true);
    }
    return $params;
}


public function saveIndexAction(){
    $post = new Post();

    $signup_params = $this->getRequestJson();

    //flatten signup parameters
    $params = array();
    foreach ($signup_params as $p) {
        if (is_array($p)) {
            foreach($p as $key=>$val) {
                $params[$key]=$val;
            }
        }
    }
}

And it worked.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.