weixin_33717298 2015-05-08 17:11 采纳率: 0%
浏览 29

Magento Ajax和Mage :: getModel

I'm using Magento 1.9 and in a page .phtml I have an AJAX request:

$('#dive').change(function() {
    if($(this).val() > 0) {
        $j.ajax({
            url: 'dominio.com/myfile.php',
            type: 'POST',
            data: { id: $(this).val() },
            success: function(data) {
                $('.classe').html(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert('error');
            }    
        });
    }
});

The content of myfile.php is:

<?php 
     $id = $_POST['id'];
     echo $id;
?>

It works well the $id is shown in .classe when the value of select form is > 0 but I want to have inside of myfile.php the subcategory of parent category with id = $id

I tried to add this code:

$children = Mage::getModel('catalog/category')->getCategories($id);
foreach ($children as $subcategory) {
    echo $subcategory->getName();
}

That code works in .phtml but if I add that in myfile.php I get nothing.

Any ideas?

  • 写回答

1条回答 默认 最新

  • lrony* 2015-05-08 17:15
    关注

    You probably miss application initialization. if this is your entire php code:

    <?php 
         $children = Mage::getModel('catalog/category')->getCategories($id);
    
    foreach ($children as $subcategory) {
          echo $subcategory->getName();
    }
    ?>
    

    you need to add Mage.php and start Mage::app();

    <?php 
    require_once ('app/Mage.php');
    Mage::app();
    $children = Mage::getModel('catalog/category')->getCategories($id);
    
    foreach ($children as $subcategory) {
          echo $subcategory->getName();
    }
    ?>
    

    it looks like you probably don't use new controller so you need to initialize the app again.

    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名