Php 如何在Joomla组件中使用jQueryAjax?

Php 如何在Joomla组件中使用jQueryAjax?,php,jquery,ajax,joomla,Php,Jquery,Ajax,Joomla,我在Joomla开发网站,同时我遇到了一个问题,请帮我解决下面的问题 这是我的组件文件夹结构 htdocs/Joomla/administrator/component/com_test/test.php,controller.php models/test.php controllers/test.php

我在Joomla开发网站,同时我遇到了一个问题,请帮我解决下面的问题

这是我的组件文件夹结构

htdocs/Joomla/administrator/component/com_test/test.php,controller.php
                                              models/test.php
                                              controllers/test.php
                                              views/test/view.html.php
                                              view/test/tmpl/default.php
现在在
view.html.php
中,我创建了一个表单,其中我使用jquery ajax代码进行usernmae可用性检查

但是我不明白如何将所有的东西结合起来,以得到usename是否可用的结果

下面是我在test/view.html.php上编写的代码

<script type="text/javascript">
 jQuery(document).ready(function(){
 jQuery("#username").change(function () {
    var usr = jQuery("#username").val();
    if (usr.length >= 2) {
     jQuery("#status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...');
     jQuery.ajax({
         type: "POST",
         url: "index.php?option=com_test&view=check_user",
         data: "username=" + usr,
         success: function (msg) {
         jQuery("#status").ajaxComplete(function (event, request, settings) {
         if (msg == 'OK') {
            jQuery("#username").removeClass('object_error'); // if necessary
                jQuery("#username").addClass("object_ok");
         }
         else {
               jQuery("#username").removeClass('object_ok'); // if necessary
               jQuery("#username").addClass("object_error");
               jQuery(this).html(msg);
         }
       });
      }
    });
  }    
});

<script>

<form action="" method="post" name="addUserForm" id="addUserForm" > 
   <table width="100%" border="0" cellpadding="4" cellspacing="2">
     <tr>
    <th >User Name :</th>
        <td ><input type="text" name="username" id="username" size="50">
             <span id="status"></span>  
        </td>
     </tr>      
   </table>
</form>
<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the advertising component
 */
class TestViewCheck_user extends JView 
{
   /**
    * Default display function
    */  
    function display($tpl = null) 
    {
        $testController = new TestController();
        // Make an object of Main Model class contains Main functions
        $testModel = $testController->getModel('test');
        $userName  = JRequest::getVar('username');
        parent::display($tpl);
        }
 }
?>
检查\u user/view.html.php中编码

<script type="text/javascript">
 jQuery(document).ready(function(){
 jQuery("#username").change(function () {
    var usr = jQuery("#username").val();
    if (usr.length >= 2) {
     jQuery("#status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...');
     jQuery.ajax({
         type: "POST",
         url: "index.php?option=com_test&view=check_user",
         data: "username=" + usr,
         success: function (msg) {
         jQuery("#status").ajaxComplete(function (event, request, settings) {
         if (msg == 'OK') {
            jQuery("#username").removeClass('object_error'); // if necessary
                jQuery("#username").addClass("object_ok");
         }
         else {
               jQuery("#username").removeClass('object_ok'); // if necessary
               jQuery("#username").addClass("object_error");
               jQuery(this).html(msg);
         }
       });
      }
    });
  }    
});

<script>

<form action="" method="post" name="addUserForm" id="addUserForm" > 
   <table width="100%" border="0" cellpadding="4" cellspacing="2">
     <tr>
    <th >User Name :</th>
        <td ><input type="text" name="username" id="username" size="50">
             <span id="status"></span>  
        </td>
     </tr>      
   </table>
</form>
<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the advertising component
 */
class TestViewCheck_user extends JView 
{
   /**
    * Default display function
    */  
    function display($tpl = null) 
    {
        $testController = new TestController();
        // Make an object of Main Model class contains Main functions
        $testModel = $testController->getModel('test');
        $userName  = JRequest::getVar('username');
        parent::display($tpl);
        }
 }
?>

但是当我运行这个代码时…为什么
http://localhost/Joomla/includes/js/joomla.javascript.js
文件运行无限次。。最后给出4个错误

现在我有什么需要修改/添加更多???请引导我

参考任何有用的链接,教创建组件一步一步…这将是非常有帮助的我


非常感谢

所有前端代码都应该在您的tmpl中,所以您的Ajax内容也应该在那里。查看本教程,了解如何为Joomla(死链接)制作MVC组件。

1-复制Joomla的main
index.php
joomlaRoot/index.php
),并将其重命名为任何名称(例如:
joomlaRoot/ajax.php

2-禁用渲染方法(
$mainframe->render();

3-在“渲染方法”行下复制此代码:

/***/
$document =& JFactory::getDocument();
$content=$document->getBuffer();
foreach($content as $var){
    foreach($var as $var2){
        echo $var2;
    }
}

/**/
我找到了解决办法。 您必须防止joomla附加模板和模块以及。。。输出ajax数据。 为此,必须在显示数据后添加此代码

//after $this->display($tpl);

global $mainframe;

$mainframe->close();

在joomla 1.7及更高版本中,您可以这样关闭它

$app = &JFactory::getApplication();
$app->close();

在ajax url中使用
format=raw
,因为这样只会显示输出,而没有任何模板。

您可以使用:

url:“index.php?option=com\u test&view=check\u user&format=raw”,

这将阻止加载整个模板,但只加载组件的输出,这是您在调用ajax函数时实际需要的


此外,您还可以检查
index2.php
(而不是
index.php
),它以类似的方式设计来提供简单的输出,而不呈现整个模板。

没错,Joomla将加载模块、组件、默认模板和Joomla核心所需的内容。。。通过使用“&format=raw”(这将强制Joomla包含Joomla的“nothing”)或“&template=your_own_template_for_ajax”(这将强制Joomla包含您自己的“nothing”)来更改此行为

我不知道“&format=raw”,所以我使用自己的空ajax模板。 空的tamplate对我来说是有意义的-我可以按照我想要的方式自定义它(例如,默认情况下包含某些内容)。“&format=raw”是一个不错的选项,但不是唯一的选项。决定取决于默认情况下你想做什么/得到什么

如何在前端制作这样的ajax模板?

您必须在前端\模板\目录内创建一个新目录(例如“ajax”)。然后将3个文件放入其中:

index.php

<jdoc:include type="component" />
...XML content.. 
<!DOCTYPE html><title></title>
有关如何正确创建templateDetails.xml的说明,请参见:

index.php

<jdoc:include type="component" />
...XML content.. 
<!DOCTYPE html><title></title>

这就是前端解决方案所需的全部内容

通过如下方式调用进行测试:

这是前端的100%工作解决方案。
后端不是我测试的。我相信您还必须为后端创建一个单独的模板。或者以某种方式到达前端模板(目前不知道如何实现)

上述所有给定的解决方案(Joomla 3.1)对我都不起作用。所以我用了这个解决方案。在url中添加tmpl=组件

index.php?option=com_photos&view=intphoto&id=1&tmpl=component

哇!谢谢你,伙计!这真的很有帮助!我让用户通过我的组件下载文件,Joomla用于在图像内容前后输出html,因此它最终变得“不可读”。添加$mainframe->close();在标题保存了一天之后!再次感谢!不幸的是,现在是死链接