php - 如何在 Joomla 组件中使用 Jquery AJAX?

标签 php jquery ajax joomla

我正在用 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 代码进行用户名可用性检查

但我不知道如何组合所有的东西来得到 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>

我已经为上面的操作创建了下面的文件夹结构,请告诉我哪里错了

view/check_user/view.html.php
views/check_user/tmpl/default.php

check_user/view.html.php 中的代码

<?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 个错误

现在我必须修改/添加更多???请指导我....

请引用任何有用的链接,教你如何一步一步地创建组件......这对我很有帮助

非常感谢

最佳答案

我找到了解决方案。 您必须防止 joomla 将模板和模块附加到您的输出 ajax 数据。 为此,您必须在显示数据后添加此代码

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

global $mainframe;

$mainframe->close();

关于php - 如何在 Joomla 组件中使用 Jquery AJAX?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3157488/

相关文章:

javascript - 如何将相同的 jquery 添加到不同的 div 但具有不同的内容?

javascript - 服务器请求 : angular route/url after hash#?

javascript - 如何在 jquery 上更改 dateTime json 的格式?

php - 数据库模式设计 : What to do about unverified invitations?

php - 两列上的MySQL内部连接

java - Android - 如何使用登录名和密码正确查询在线数据库?

php - 测试 CodeIgniter session 变量的正确方法是什么?

jquery - 使用 jQuery 动画回到原始位置

jquery - 滑出 div,使用 .show ignones {position :absolute; bottom:0px;} in chrome & safari, works in firefox

jquery - 如何使用 Jquery AJAX 将 API key 作为请求 header 发布到 API 网关