weixin_33724046 2015-08-16 06:01 采纳率: 0%
浏览 28

magento中的AJAX示例

Hello everyone,

I am a newbie to Magento. I want to learn **ajax process in Magento.** Can anyone help me to understand ajax in Magento with one simple example? Your help will be highly appreciated.

  • 写回答

1条回答 默认 最新

  • ?Briella 2015-08-17 06:11
    关注

    I give you a simple example for you. To work with basic jQuery Ajax in Magento you have work in phtml page and Controller. Just add the script in phtml page:

    <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery(".like-result").click(function() {
                //alert(this.id);
                var id = this.id;
                //alert(custid);
                jQuery(".notify-status").hide();
                jQuery(".notify-loader").show();
                jQuery.ajax({      
                type: "POST",
                data: 'pid=' + id,
                url:'http://192.168.2.3/subhranil-demo/blog/index/likecount',
                success:function(response){                       
                    if (response) {
                        jQuery(".notify-loader").hide();
                        jQuery(".notify-status").show();
                        jQuery("#un"+id).html(response);
                    }
                }
                });
            });
         });
    </script>
    

    In the above script under jQuery.ajax you can also see type, data, url. type is used for sending process like POST or GET; in data, you will send information to the controller; in URL, you can declare the controller path. Here I have a 'blog' module and I write the public function under 'index' controller and I give the function name 'likecount'. Also here my base path is http://192.168.2.3/subhranil-demo/. So I add the link to URL as following structure: http://192.168.2.3/subhranil-demo/blog/index/likecount. Now I go to 'IndexController.php' in my controller's folder of blog module and open it. Under the class I add the following function:

    public function likecountAction()
        {
        $blogload = Mage::getModel('blog/blog')->load($_POST['pid']);
        $newid = $blogload['like']+1;
        $data = array('like'=> $newid);
        $blogload->addData($data);
        try {
            $blogload->setId($_POST['pid'])->save();
            echo $newid;
    
        } catch (Exception $e){
            echo $e->getMessage(); 
        }
        }
    

    Here in the Blog Database, I have the fields like pid (as a primary key) and like. the function works like that when you click on 'like-result' class the like increase +1. My div structure also like that:

    <?php
    
        $allCollection=Mage::getModel("blog/blog")->getCollection();
        $allCollection->addFieldToFilter('status',1);
        if ($allCollection->count() >= 1)
        {
            $news = array();
            ?>
            <div class="blog clearfix">
            <?php
            foreach ($allCollection as $news)
            {?>   
    <p class="like-result" id="<?php echo $news->getId(); ?>"> <?php echo $news->getLike(); ?> </p>
        <a style="display: none;" class="notify-loader"><img src="http://www.sendhersomething.com/skin/frontend/megatron/default/images/ajax/notify-loader.gif"></a>
        <a style="display: none;" class="notify-status"><img src="http://www.sendhersomething.com/skin/frontend/megatron/default/images/ajax/ststus.png"></a>
      <?php } ?>
            </div>
    <?php } ?>
    

    Try this!

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作