【发布时间】:2012-10-19 17:09:46
【问题描述】:
我目前正在将一些功能构建到 Joomla 构建中,而结果是在选择框更改时从数据库返回的。
我遇到的问题是当我进行Ajax 调用时,我得到一个Class JFactory not found' 错误,我认为这是因为运行php 代码的文件在AJAX 时不被认为在Joomla 框架内打电话了吗?
我试图自己解决这个问题,但遇到了麻烦。
谁能指出我在哪里出错的正确方向?
Ajax 调用(在 index.php 中):
<script type="text/javascript">
var url = "<?php echo $this->baseurl; ?>/templates/<?php echo $this->template; ?>/directory_code.php";
jQuery(document).ready(function() {
jQuery('#city').change(function() {
jQuery.ajax({
url: url,
type: "POST",
data: jQuery("#city").serialize(),
dataType: 'json',
success: function(data) {
alert('data');
}
});
});
});
</script>
以及函数(在一个名为 directory_code.php 的文件中):
if(isset($_POST)) {
if(isset($_POST['city'])) {
$city = $_POST['city'];
$suburbs = populateSuburbs($city);
}
}
function populateSuburbs($city) {
$db = JFactory::getDBO();
$query = "SELECT DISTINCT suburb FROM vyj20_contact_details";
$db->setQuery($query);
$results = $db->loadObjectList();
foreach($results as $key => $result) {
$data['suburb'][] = array(
$key => $result->suburb
);
}
}
非常感谢
【问题讨论】:
-
查看这个 joomla 文档-docs.joomla.org/Adding_AJAX_to_your_component