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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?