Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have an issue with symfony2 when i use ajax to set session, hope u pro guys can help me.

here is my **controller code:**


PHP
//show month event list
    public function indexAction() {
        if ($this->getRequest()->isXmlHttpRequest()) {
            $paging = $this->getRequest()->get("nom");
            $session = $this->getRequest()->getSession();

            if ($paging) {
                //if $paging is set, then that's a click pager ajax event
                //(not 1st time load)
                $year = $paging;
                $session->set('year', $year);
            } else {
                //$paging is null, it's the first time page load
                $year = (new \DateTime())->format("Y");
                $session->set('year', $year);
            }
            $repository = $this
                    ->getDoctrine()
                    ->getManager()
                    ->getRepository('HycAccountBundle:MonthEvent');

            $annuallist = $repository->monthListByYear($year);
            $jsonlist = json_encode($annuallist);
            return new Response($jsonlist);
        }
        
        //this part is to return entity to twig for using after
        $em = $this->getDoctrine()->getManager();
        $allimages = $em->getRepository('HycAccountBundle:TypeImage')
                ->findAll();
        return $this->render('HycAccountBundle:Account:index.html.twig', array('allimages' => $allimages));
    }



here is my **twig code:**



JavaScript
<script type="text/javascript">
        $(function() {
            $(document).ready(function (){
             jQuery.ajax({
                    type: 'GET',
                    cache: false,
                    url: "{{ path('hyc_account_homepage') }}",
                    success: function(data, textStatus, jqXHR) {
                        alert({{app.session.get('year')}});
                        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                        //!!!!!!!!! here i can get year 2014 !!!!!!!!!!!!!!
                        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    },
                    error:function (){
                    alert('error!');
                }
                });
                return false;
            });
        });
    <script>


but when i click pager part, for example, i click year 2013, then ajax call will return a number (data: 'nom=' + num) to controller, but there i reset session value to num normally, but i cant get alert correct session (it's always 2014 as the 1st time) here is the code ajax, almost same as above:

JavaScript
<script type="text/javascript">
       $(document).ready(function(){
           //after paging, reload month
           $('#page-selection').bootpag({
               total: 3000,
               page: 2014,
               maxVisible: 5
            }).on('page', function(event, num){
                jQuery.ajax({
                   type: 'GET',
                   url: "{{ path('hyc_account_homepage') }}",
                   data: 'nom=' + num,
                   success: function(data, textStatus, jqXHR) {
                       //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                       //here is the problem, it's always 2014, not set again !!
                       //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                       alert({{app.session.get('year')}});


                   },
                   error:function (){
                   alert('error!');
               }
               });
               return false;
            });
       });
   </script>


hope u guys help me, thanks in advance, i've tried for almost 1 day and looked for almost all in google but find nothing = =
Posted

1 solution

try
JavaScript
data: {nom: num}
 
Share this answer
 
Comments
v03051435 21-Aug-14 4:41am    
i guess i cant get updated value from alert({{app.session.get('year')}});?? cause it's server side code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900