带有Ajax的Joomla:致命错误:找不到类'JFactory' [英] Joomla with Ajax: Fatal error: Class 'JFactory' not found
问题描述
在弄清楚如何一起使用Ajax和Joomla框架时仍然遇到问题.我已经创建了一个Joomla组件,可以使用它进行访问:
Still having problems figuring out how to use Ajax and the Joomla framework together. I've created a Joomla component which I can access with:
index.php?option=com_mycomponent
我在components/com_mycomponent/views/mycomponent/tmpl/default.php
中使用带有Ajax的Jquery:
I'm using Jquery with Ajax in components/com_mycomponent/views/mycomponent/tmpl/default.php
:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
<html>
<head>
<title>Ajax with jQuery Example</title>
<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/JavaScript">
$(document).ready(function(){
$("#generate").click(function(){
$("#quote p").load("components/com_mycomponent/views/mycomponent/tmpl/script.php");
});
});
</script>
</head>
<body>
<div id="wrapper">
<div id="quote"><p> </p></div>
<input type="submit" id="generate" value="Generate!">
</div>
</body>
</html>
然后在script.php文件中,我具有以下内容:
Then in the script.php file I have this:
<?php
$user =& JFactory::getUser();
echo "This is the user: ".$user;
?>
如果我不将任何Joomla框架代码放在script.php中,则可以正常工作.但是,这样做的目的是我需要使用Joomla框架,因此需要使用创建组件的整个过程.但是我仍然不明白如何构造Joomla组件,这样就不会出现Class 'JFactory' not found
错误?
If I don't put any Joomla framework code in script.php it works fine. But the purpose of doing this is that I need to use the Joomla framework and hence the whole point of creating a component. But I still don't understand how I need to structure the Joomla component so I don't get the Class 'JFactory' not found
error?
推荐答案
最终并没有花费太长时间.唯一需要的文件是default.php,因此您可以删除script.php和其中的其他奇数文件.
Didn't end up taking too long in the end. The only file that is required is the default.php, so you can delete the script.php and other odd files you have in there.
Default.php
<?php
defined('_JEXEC') or die('Restricted access');
//$document =& JFactory::getDocument();
//$document->addScript("https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
$user =& JFactory::getUser();
?>
<div id="wrapper">
<div id="quote" style="display:none; padding-bottom:10px;">
<?php
echo "<p>This is your username: " . $user->username . "</p>";
echo "<p>This is your realname: " . $user->name . "</p>";
echo "<p>This is your user ID: " . $user->id . "</p>";
?>
</div>
<input type="submit" id="generate" value="Generate!">
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#generate").click(function(){
$("#quote").show();
});
});
</script>
我已经注释掉了代码中的jquery参考,因为该widgetkit已经在加载它的副本,但是只是为了以防万一.
I have commented out the jquery reference in the code, seeing as the widgetkit is already loading a copy of it, but kept it there just incase.
它不会加载另一个文件,而是隐藏#quote
div标签,并且单击按钮时,它会显示其中的数据以及其中的数据.在此示例中,我添加了realname
和user ID
以防万一.
Instead of loading another file, what this does is, it hides #quote
div tag and when the button is clicked, it reveals it with the data inside it. In this one, I have added realname
and user ID
just incase.
希望这会有所帮助.
致谢
这篇关于带有Ajax的Joomla:致命错误:找不到类'JFactory'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!