我正在使用Magento1.9,在一个页面.phtml中,我有一个AJAX请求:
$('#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');
}
});
}
});myfile.php的内容如下:
<?php
$id = $_POST['id'];
echo $id;
?>它工作得很好,当选择表单的值>0时,$id在.classe中显示,但我希望在myfile.php中包含父类别的子类别id = $id
我试图添加以下代码:
$children = Mage::getModel('catalog/category')->getCategories($id);
foreach ($children as $subcategory) {
echo $subcategory->getName();
}该代码在.phtml中工作,但如果我在myfile.php中添加该代码,则什么也得不到。
有什么想法吗?
发布于 2015-05-08 17:15:28
您可能错过了应用程序初始化。如果这是完整的php代码:
<?php
$children = Mage::getModel('catalog/category')->getCategories($id);
foreach ($children as $subcategory) {
echo $subcategory->getName();
}
?>您需要添加Mage.php并启动Mage::app();
<?php
require_once ('app/Mage.php');
Mage::app();
$children = Mage::getModel('catalog/category')->getCategories($id);
foreach ($children as $subcategory) {
echo $subcategory->getName();
}
?>看起来你可能不使用新的控制器,所以你需要再次初始化应用程序。
https://stackoverflow.com/questions/30129301
复制相似问题