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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3157488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:53:08  来源:igfitidea点击:

how to use Jquery AJAX in Joomla Components?

phpjqueryajaxjoomla

提问by JustLearn

i m developing site in Joomla, meanwhile i stuck in a problem,please help me in below problem

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

here is my folder structure for component

这是我的组件文件夹结构

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

now in view.html.phpi created a form where i m using jquery ajax code for usernmae availability check

现在view.html.php我创建了一个表单,其中我使用jquery ajax 代码进行 usernmae 可用性检查

but i m not getting how do i combine all things to get the result that usename is available or not

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

here is my code written on test/view.html.php

这是我写在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>


i have created below folders structure for above action , please tell me where do i mistake

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

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

code in check_user/view.html.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);
        }
 }
?>

but when i run this code...why http://localhost/Joomla/includes/js/joomla.javascript.jsfile runs infinite times.. and finally give 4 error

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

now what i have to modify/add more??? please just guide me ....

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

refer any useful link which teach to create component step by step ...it will be very helpful for me

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

Thanks a lot

非常感谢

采纳答案by Martin

All front end code should be in your tmpl, so your Ajax stuff should be in there too. Check this tutorial out on how to make MVC components for Joomla http://www.joomladevuser.com/tutorials/components(deadlink).

所有前端代码都应该在您的 tmpl 中,因此您的 Ajax 内容也应该在那里。查看本教程,了解如何为 Joomla http://www.joomladevuser.com/tutorials/components(死链接)制作 MVC 组件。

回答by muhammad sherafat

I found the solution. You must prevent the joomla to attach the templates and modules and ... to your output ajax data. for this you must add this code after displaying your data

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

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

global $mainframe;

$mainframe->close();

回答by Alex

In joomla 1.7 and up you can close it like this

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

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

回答by Paulius

That is right, Joomla will load modules, component, whatewer Your default template and Joomla core requires... Change this behavior by using "&format=raw" (this will force Joomla to include Joomla`s "nothing") or "&template=your_own_template_for_ajax" (this will force Joomla to include Your own "nothing").

是的,Joomla 将加载模块、组件等等。您的默认模板和 Joomla 核心需要...通过使用“&format=raw”(这将强制 Joomla 包含 Joomla 的“nothing”)或“&template= your_own_template_for_ajax”(这将强制 Joomla 包含您自己的“无”)。

I didn`t knew about "&format=raw" so I use my own empty template for ajax. Empty tamplate makes sense for me - I can customize it the way I want (e.g. to include something by default). "&format=raw" is the good option, but not the only one. The decision depends on what You wanna do/get by default.

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

How to make such ajax template in front-end?

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

You must create a new directory (e.g. "ajax") inside front-end \templates\ directory. Then put 3 files inside:

您必须在前端\templates\ 目录中创建一个新目录(例如“ajax”)。然后在里面放3个文件:

index.php:

索引.php:

<jdoc:include type="component" />

templateDetails.xml:

模板详细信息.xml

...XML content.. 

Instructions on how to properly create templateDetails.xml can be found here:
http://docs.joomla.org/Creating_a_basic_templateDetails.xml_file

可以在此处找到有关如何正确创建 templateDetails.xml 的说明:http:
//docs.joomla.org/Creating_a_basic_templateDetails.xml_file

index.php:

索引.php:

<!DOCTYPE html><title></title>

That is all You need for the front-end solution.

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

Test it by calling like this: http://www.example.com/index.php?template=ajax

通过像这样调用来测试它:http: //www.example.com/index.php?template=ajax

This is 100% working solution for the front-end. Back-end is not tested by me. I believe You would have to create a separate template for back-end also. Or reach the front-end template somehow (currently have no ideas on how to)...

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

回答by RN Kushwaha

Nothing worked for me from all given solutions above(Joomla 3.1). So I used this solution. Add tmpl=component in url.

上面所有给定的解决方案(Joomla 3.1)对我没有任何作用。所以我使用了这个解决方案。在 url 中添加 tmpl=component。

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

回答by muhammad sherafat

1 - copy main index.php(joomlaRoot/index.php) of joomla and rename it to any name (for example: joomlaRoot/ajax.php).

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

2 - disable render method ($mainframe->render();)

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

3 - copy this code under the render method line:

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

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

/**/

回答by sathish

use format=rawin the ajax url, as this would just show the output without any template.

使用format=raw在AJAX网址,因为这将只显示没有任何模板输出。

回答by burek pekaric

You can use:

您可以使用:

url: "index.php?option=com_test&view=check_user&format=raw",

url: "index.php?option=com_test&view=check_user&format=raw",

which will prevent loading of entire template, but will load only component's output, which you actually want when you call your ajax functions.

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

Also, you might check index2.php(instead of index.php) which has been designed in a similar fashion to provide the simple output, without rendering entire template.

此外,您可能会检查index2.php(而不是index.php)以类似方式设计以提供简单输出,而无需渲染整个模板。