2015-05-08 31 views
0

我使用Magento的1.9,並在頁面一個.phtml我有一個AJAX請求:Magento的阿賈克斯和法師:: getModel

$('#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; 
?> 

它的效果很好$ .classe當選擇表格的值是> 0時顯示,但我希望在myfile.php裏面有id = $ id的父類別的子類別

我試圖添加此代碼:

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

該代碼在.phtml中工作,但是如果我在myfile.php中添加該代碼,我什麼也得不到。

任何想法?

回答

1

您可能會錯過應用程序初始化。 如果這是你的整個PHP代碼:

<?php 
    $children = Mage::getModel('catalog/category')->getCategories($id); 

foreach ($children as $subcategory) { 
     echo $subcategory->getName(); 
} 
?> 

你需要添加Mage.php 並開始法師::應用程序();

<?php 
require_once ('app/Mage.php'); 
Mage::app(); 
$children = Mage::getModel('catalog/category')->getCategories($id); 

foreach ($children as $subcategory) { 
     echo $subcategory->getName(); 
} 
?> 

它看起來像你可能不使用新的控制器,所以你需要重新初始化應用程序。

+0

我添加了mage.php和star Mage :: app();像你說的,但我仍然得到ajax請求的錯誤信息 – Xavier

+0

你能粘貼錯誤信息嗎? – MagentoNinja

+0

這是我設置爲ajax腳本的消息「alert('error');」 – Xavier