PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 15, 2022

[FIXED] jquery ajax is not working in default.ctp file of cakephp 3.x layout

 March 15, 2022     ajax, cakephp-3.0, jquery     No comments   

Issue

I want to call a function of cakephp controller using jquery ajax from my layout file default.ctp and want to set return response in notification section of every page but its not working.

This is my controller method:

public function cartItemCount(){    
    $conn = ConnectionManager::get('default');
    $result = $conn->query("SELECT count(*) as total_count FROM carts")->fetchAll('assoc');
    $this->response->body($result[0]['total_count']);
    return $this->response;
}

This is my jquery code:

<script type="text/javascript">
    $(document).ready(function(){   
        $.get('<?php echo SITEURL;?>/NewOrders/cartItemCount',function(res){
            console.log(res);
            $(".cart_item").text(res);  
        });
    });
</script> 

When I write this script in each and every view its working correct but I want write this script in default view so its work for all view without writing code in every view file. Please someone help me out.


Solution

You can store total count in session and then retrieve that session where you want to call.



Answered By - Kishor
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing